7 int allowbits[Brdsize] = {
19 int boxind[Brdsize][Brdsize] = {
20 {0,1,2,9,10,11,18,19,20},
21 {3,4,5,12,13,14,21,22,23},
22 {6,7,8,15,16,17,24,25,26},
23 {27,28,29,36,37,38,45,46,47},
24 {30,31,32,39,40,41,48,49,50},
25 {33,34,35,42,43,44,51,52,53},
26 {54,55,56,63,64,65,72,73,74},
27 {57,58,59,66,67,68,75,76,77},
28 {60,61,62,69,70,71,78,79,80},
30 int colind[Brdsize][Brdsize] = {
31 {0,9,18,27,36,45,54,63,72},
32 {1,10,19,28,37,46,55,64,73},
33 {2,11,20,29,38,47,56,65,74},
34 {3,12,21,30,39,48,57,66,75},
35 {4,13,22,31,40,49,58,67,76},
36 {5,14,23,32,41,50,59,68,77},
37 {6,15,24,33,42,51,60,69,78},
38 {7,16,25,34,43,52,61,70,79},
39 {8,17,26,35,44,53,62,71,80},
41 int rowind[Brdsize][Brdsize] = {
43 {9,10,11,12,13,14,15,16,17},
44 {18,19,20,21,22,23,24,25,26},
45 {27,28,29,30,31,32,33,34,35},
46 {36,37,38,39,40,41,42,43,44},
47 {45,46,47,48,49,50,51,52,53},
48 {54,55,56,57,58,59,60,61,62},
49 {63,64,65,66,67,68,69,70,71},
50 {72,73,74,75,76,77,78,79,80},
61 for(i = 0; i < Psize; i++) {
62 if(i > 0 && i % Brdsize == 0)
64 print("%2.2d ", board[i] & Digit);
84 int row = getrow(cell);
85 int col = getcol(cell);
87 return 3*(row/3)+ col/3;
91 setdigit(int cc, int num)
93 board[cc] = (board[cc] & ~Digit) | num;
99 int i,j,d,sum,last,last2;
101 for (i = 0; i < 9; i++) {
102 for (d = 0;d < 9; d++) {
106 for (j = 0; j < 9; j++) {
107 if (board[boxind[i][j]] & allowbits[d]) {
112 sum += ((board[boxind[i][j]] & Solve)==(d << 4)) ? 1: 0;
116 if ((sum==1)&&(last>=0))
117 if (!setallowed(board,last,d))
120 if((sum == 2) && (last >= 0) && ( last2 >= 0) &&
121 (getrow(last) == getrow(last2))) {
122 for (j = 0; j < 9; j++) {
123 int c = rowind[getrow(last)][j];
124 if ((c != last)&&(c != last2)) {
125 if (board[c] & allowbits[d]) {
126 board[c] &= ~allowbits[d];
127 if ((board[c] & Allow)==0)
133 if((sum == 2) && (last >= 0) && (last2 >= 0) &&
134 (getcol(last) == getcol(last2))) {
135 for (j = 0;j <9;j++) {
136 int c = colind[getcol(last)][j];
137 if ((c != last) && (c != last2)) {
138 if (board[c] & allowbits[d]) {
139 board[c] &= ~allowbits[d];
140 if ((board[c] & Allow) == 0)
156 for (i = 0; i < 9; i++) {
157 for (d = 0; d < 9; d++) {
160 for (j = 0; j <9 ; j++) {
161 if (board[rowind[i][j]] & allowbits[d]) {
165 sum += ((board[rowind[i][j]] & Solve) == (d << 4)) ? 1: 0;
169 if ((sum == 1) && (last >= 0)) {
170 if (!setallowed(board, rowind[i][last], d))
183 for (i = 0; i < 9; i++) {
184 for (d = 0; d < 9; d++) {
187 for (j = 0;j < 9;j++) {
188 if (board[colind[i][j]] & allowbits[d]) {
192 sum += ((board[colind[i][j]] & Solve) == (d << 4)) ? 1: 0;
196 if ((sum == 1) && (last >= 0)) {
197 if (!setallowed(board, colind[i][last], d))
206 setallowed(int *board, int cc, int num)
212 board[cc] = (board[cc] & ~Solve) | (num << 4);
215 for (j = 0; j < 9; j++) {
216 if (board[rowind[row][j]] & allowbits[num]) {
217 board[rowind[row][j]] &= ~allowbits[num];
218 if ((board[rowind[row][j]] & Allow) == 0)
224 for (j = 0; j < 9; j++) {
225 if (board[colind[col][j]] & allowbits[num]) {
226 board[colind[col][j]] &= ~allowbits[num];
227 if ((board[colind[col][j]] & Allow) == 0)
233 for (j = 0;j < 9;j++) {
234 if (board[boxind[box][j]] & allowbits[num]) {
235 board[boxind[box][j]] &= ~allowbits[num];
236 if ((board[boxind[box][j]] & Allow)==0)
241 for (j = 0;j < 81; j++)
242 for (d = 0; d < 9; d++)
243 if ((board[j] & Allow) == allowbits[d])
244 if (!setallowed(board, j, d))
247 if (!boxcheck(board)||!rowcheck(board)||!colcheck(board))
250 for (j = 0; j < 81; j++)
251 for (d = 0; d < 9; d++)
252 if ((board[j] & Allow) == allowbits[d])
253 if (!setallowed(board, j, d))
260 chksolved(int *board)
264 for (i = 0; i < Psize; i++)
265 if ((board[i] & Allow) != 0)
279 for (i=(s+1)%81;i!=s;i=(i+1)%81) {
280 if (!(board[i] & Allow)) {
281 d=(board[i] & Solve) >> 4;
282 if ((board[i] & Digit)!=d)
290 attempt(int *pboard, int level)
296 if (level > maxlevel)
300 exits("level"); /* too much */
303 for (i = (s + 1) % Psize; i != s; i = (i + 1) % Psize) {
304 if ((pboard[i] & Allow) != 0) {
306 for (j = (e + 1) % 9; j != e; j = (j + 1) % 9) {
307 if (pboard[i] & allowbits[j]) {
308 for (k = 0; k < Psize; k++)
311 if (setallowed(tb, i, j)) {
312 tb[i] = (tb[i] & ~Digit) | j;
314 for (k = 0;k < Psize; k++)
319 attempt(tb, level + 1);
321 for (k = 0; k < Psize; k++)
342 for (i = 0; i < Psize; i++)
343 pboard[i] = Allow | Solve | Digit;
345 for (i = 0; i < Psize; i++) {
347 c = board[i] & Digit;
348 if ((0 <= c) && (c < 9)) {
349 if (!setallowed(pboard,i,c)) {
350 print("starting position impossible... failed at cell %d char: %d\n", i, c + 1);
357 for (i = 0; i < Psize; i++)
358 board[i] = (board[i] & ~Solve) | (pboard[i] & Solve);
363 checklegal(int cc, int d)
365 int hold = board[cc];
366 int j, row, col, box;
370 for (j = 0; j < Brdsize; j++)
371 if ((board[rowind[row][j]] & Digit) == d) {
377 for (j = 0; j < Brdsize; j++)
378 if ((board[colind[col][j]] & Digit) == d) {
384 for (j = 0; j < Brdsize; j++)
385 if ((board[boxind[box][j]] & Digit) == d) {
398 for(i = 0; i < Psize; i++) {
399 board[i] = (Allow | Solve | Digit);
413 for (d = 0; d < Brdsize; d++) {
415 if (board[i] & allowbits[d]) {
416 setallowed(board, i, d);
417 board[i] = (board[i] & ~Digit) | d;
423 for (i = 0; i < Psize; i++) {
424 if ((0 <= (board[i] & Digit)) && ((board[i] & Digit) < 9))
426 setdigit(i, board[i] & Digit);
430 fprint(2, "failed to make puzzle\n");
431 exits("failed to make puzzle");