-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsudoku_solver.c
More file actions
363 lines (326 loc) · 8.7 KB
/
sudoku_solver.c
File metadata and controls
363 lines (326 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//#include <stddef.h>
//#include <stdlib.h>
//#include <string.h>
//#include <stdio.h>
//#include <time.h>
// sudoku board size
int blockSize = 3;
int size = 3 * 3;
typedef int Board[9][9];
int called = 0;
void *NULL = 0;
typedef struct {
int *possibilities;
int size;
} Possibilities;
Possibilities *possibleNumbersAt(Board *b, int i, int j);
int isSolved(Board *b);
void printBoard(Board *b);
void freePossibilitiesMap(Possibilities ***map) {
int i;
int j;
for (i = 0; i < size; ++i) {
if (!map[i]) break;
for (j = 0; j < size; ++j) {
if (!map[i][j]) {
continue;
}
free(map[i][j]->possibilities);
free(map[i][j]);
}
free(map[i]);
}
}
// Solves the sudoku game, returns the board if successfully solved the game.
Board *Solve(Board *b) {
++called;
Possibilities ***possibilitiesMap = calloc(9, sizeof(Possibilities**));
int i;
int j;
Possibilities **row;
Possibilities *possibilities;
for (i = 0; i < size; ++i) {
possibilitiesMap[i] = calloc(9, sizeof(Possibilities*));
row = possibilitiesMap[i];
for (j = 0; j < size; ++j) {
if ((*b)[i][j] != 0) {
continue;
}
// check possible numbers for each unfilled square
// if any of them were not fillable, then return immediately
possibilities = possibleNumbersAt(b, i, j);
int *p = possibilities->possibilities;
if (possibilities->size == 0) {
freePossibilitiesMap(possibilitiesMap);
return NULL;
}
row[j] = possibilities;
}
}
// fill if there's only one possibility
Board *solvedBoard;
for (i = 0; i < size; ++i) {
row = possibilitiesMap[i];
for (j = 0; j < size; ++j) {
if ((*b)[i][j] != 0) {
continue;
}
possibilities = row[j];
if (possibilities->size == 1) {
(*b)[i][j] = possibilities->possibilities[0];
// fill the number and check
solvedBoard = Solve(b);
if (solvedBoard != NULL) {
return solvedBoard;
}
// not solvable
(*b)[i][j] = 0;
freePossibilitiesMap(possibilitiesMap);
return NULL;
}
}
}
// if multiple numbers are possible, check each of them one by one
for (i = 0; i < size; ++i) {
row = possibilitiesMap[i];
for (j = 0; j < size; ++j) {
if ((*b)[i][j] != 0) {
continue;
}
possibilities = row[j];
if (possibilities->size > 1) {
int k;
for (k = 0; k < possibilities->size; ++k) {
int num = possibilities->possibilities[k];
(*b)[i][j] = num;
solvedBoard = Solve(b);
if (solvedBoard != NULL) {
return solvedBoard;
}
}
// not solvable
(*b)[i][j] = 0;
freePossibilitiesMap(possibilitiesMap);
return NULL;
}
}
}
// if none of above code returned true of false, then all numbers must have been filled
if (isSolved(b)) {
return b;
} else {
freePossibilitiesMap(possibilitiesMap);
return NULL;
}
}
// Checks row validity
int checkRowValidity(Board *b, int i) {
char checked[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int col;
for (col = 0; col < size; ++col) {
int num = (*b)[i][col];
if (num == 0) {
continue;
}
if (checked[num-1]) {
return 0;
}
checked[num-1] = 1;
}
return 1;
}
// Checks column validity
int checkColumnValidity(Board *b, int j) {
char checked[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int row;
for (row = 0; row < size; ++row) {
int num = (*b)[row][j];
if (num == 0) {
continue;
}
if (checked[num-1]) {
return 0;
}
checked[num-1] = 1;
}
return 1;
}
// Checks block validity of the given coords (NOT block-wise coords)
int checkBlockValidity(Board *b, int i, int j) {
int blockI = i / 3;
int blockJ = j / 3;
char checked[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
for (i = blockI * 3; i < (blockI+1)*3; ++i) {
for (j = blockJ * 3; j < (blockJ+1)*3; ++j) {
int num = (*b)[i][j];
if (num == 0) {
continue;
}
if (checked[num-1]) {
return 0;
}
checked[num-1] = 1;
}
}
return 1;
}
// Checks the entire board is filled and is valid
int isSolved(Board *b) {
// check if all is filled
int i;
int j;
for (i = 0; i < size; ++i) {
for (j = 0; j < size; ++j) {
if ((*b)[i][j] == 0) {
return 0;
}
}
}
// check blocks
for (i = 0; i < blockSize; ++i) {
for (j = 0; j < blockSize; ++j) {
if (!checkBlockValidity(b, i*blockSize, j*blockSize)) {
return 0;
}
}
}
// check rows
for (i = 0; i < size; ++i) {
if (!checkRowValidity(b, i)) {
return 0;
}
}
// check columns
for (j = 0; j < size; ++j) {
if (!checkColumnValidity(b, j)) {
return 0;
}
}
return 1;
}
Possibilities *possibleNumbersAt(Board *b, int i, int j) {
char taken[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
// row
int col;
int num;
for (col = 0; col < size; ++col) {
num = (*b)[i][col];
if (num == 0) {
continue;
}
taken[num-1] = 1;
}
// column
int row;
for (row = 0; row < 9; ++row) {
num = (*b)[row][j];
if (num == 0) {
continue;
}
taken[num-1] = 1;
}
// block
int blockI = i / 3;
int blockJ = j / 3;
for (row = blockI * 3; row < (blockI+1)*3; ++row) {
for (col = blockJ * 3; col < (blockJ+1)*3; ++col) {
num = (*b)[row][col];
if (num == 0) {
continue;
}
taken[num-1] = 1;
}
}
Possibilities *possibilities = calloc(1, sizeof(Possibilities));
int k;
int count = 0;
for (k = 0; k < size; ++k) {
if (!taken[k]) {
++count;
}
}
possibilities->size = count;
int *p = calloc(1, count * sizeof(int));
possibilities->possibilities = p;
count = 0;
for (k = 0; k < size; ++k) {
if (!taken[k]) {
p[count] = k + 1;
++count;
}
}
return possibilities;
}
Board *NewBoard(char **input) {
Board *board = calloc(1, sizeof(Board));
int i;
for (i = 0; i < size; ++i) {
char *line = input[i];
if (strlen(line) != 9) {
printf("invalid length at line %d, please input a 9x9 sudoku Board\n", i+1);
return NULL;
}
int j;
for (j = 0; j < size; ++j) {
if (line[j] == '_') {
(*board)[i][j] = 0;
} else {
(*board)[i][j] = line[j] - '0';
}
}
}
return board;
}
// Prints the board, printing '_' for blanks.
void printBoard(Board *b) {
int i;
int j;
for (i = 0; i < 9; ++i) {
for (j = 0; j < 9; ++j) {
int num = (*b)[i][j];
if (num == 0) {
printf("_");
} else {
printf("%d", num);
}
}
printf("\n");
}
}
int main() {
printf("Please input a 9x9 sudoku board:\n");
char *input[9] = {
"__42_36__",
"1_______7",
"_________",
"__26_93__",
"_8__3__5_",
"__34_28__",
"_________",
"3_______6",
"__65_42__"
};
// process #include <stdio.h> correctly to read from stdin
// int i;
// for (i = 0; i < size; ++i) {
// input[i] = calloc(11, sizeof(char));
// if (!fgets(input[i], 11, stdin)) {
// printf("failed to read line %d\n", i + 1);
// exit(1);
// }
// // null terminator
// input[i][strcspn(input[i], "\n")] = 0;
// }
Board *board = NewBoard(input);
printf("initialized board:\n");
printBoard(board);
printf("Solving...\n");
Board *solvedBoard = Solve(board);
if (solvedBoard != NULL) {
printf("Solved!\n");
printf("Called Solve() %d times\n", called);
printBoard(solvedBoard);
} else {
printf("Not solvable...\n");
}
}