-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.c
299 lines (256 loc) · 8.06 KB
/
solver.c
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
// Sudoku solver
/*
TODO: check for only place where a number could be in row/col/square, instead of just only value that could be in cell
Might have to check last square to make sure we have valid solution when there are no open square remaining?
*/
#include <stdio.h>
#define SIZE 9
#define ROOT_SIZE 3
typedef struct {
int value;
int possVals[SIZE];
int possibilities;
} cell;
int move(cell*);
int chooseMove(cell* board, int* row, int* col);
void printBoard(cell*);
int applyMove(cell* board, int* adjustments, int row, int col, int value);
int undoMove(cell* board, int* adjustments, int row, int col, int value);
int solve(cell*);
void analyze(cell*);
void findPossibilities(cell* board, int row, int col);
int main(void) {
cell board[SIZE*SIZE];
/*
int startVals[81] = {0, 0, 3, 0, 2, 0, 6, 0, 0,
9, 0, 0, 3, 0, 5, 0, 0, 1,
0, 0, 1, 8, 0, 6, 4, 0, 0,
0, 0, 8, 1, 0, 2, 9, 0, 0,
7, 0, 0, 0, 0, 0, 0, 0, 8,
0, 0, 6, 7, 0, 8, 2, 0, 0,
0, 0, 2, 6, 0, 9, 5, 0, 0,
8, 0, 0, 2, 0, 3, 0, 0, 9,
0, 0, 5, 0, 1, 0, 3, 0, 0};
for(int i=0; i<SIZE*SIZE; i++) {
board[i].value = startVals[i];
}
*/
FILE* sudokuFile = fopen("sudoku.txt", "r");
if(!sudokuFile) {
printf("File not found. Exiting.");
return -1;
}
int c;
int cellIndex = 0;
int puzzle = 1;
int ignoreLine = 0;
int pEulerSoln = 0;
while((c = fgetc(sudokuFile)) != EOF) {
if(ignoreLine) {
if(c == '\n')
ignoreLine = 0; // Reached end of text line, stop ignoring chars
}
else if(c>='0' && c<='9') {
// Char is an ascii digits
c -= '0'; // Translate from ascii char to binary value
board[cellIndex++].value = c;
if(cellIndex == SIZE*SIZE) {
// We have read in a complete puzzle
printf("\nInput:\n\n");
printBoard(board);
if(!solve(board))
printf("Puzzle %i: Solved\n", puzzle);
else
printf("Puzzle %i: No Solution Found\n", puzzle);
printf("\nOutput:\n\n");
printBoard(board);
pEulerSoln += board[0].value*100 + board[1].value*10 + board[2].value;
cellIndex = 0; // Reset index to first
puzzle++;
}
}
else if(c != '\n') {
// char is not a digit or linefeed, so we need to ignore rest of line
ignoreLine = 1;
}
}
fclose(sudokuFile);
return 0;
}
int solve(cell* board) {
analyze(board); // Initial analysis of board
return move(board); // Start backtracking algorithm
}
void analyze(cell* board) {
// Initialize possible values for each cell
for(int row=0; row<SIZE; row++) {
for(int col=0; col<SIZE; col++) {
for(int i=0; i<SIZE; i++) {
board[row*SIZE + col].possVals[i] = i+1;
}
}
}
// Remove impossible values (numbers which are already used
// elsewhere in row/column/square
for(int row=0; row<SIZE; row++) {
for(int col=0; col<SIZE; col++) {
if(!board[row*SIZE+col].value) {
findPossibilities(board, row, col);
}
}
}
}
void findPossibilities(cell* board, int row, int col) {
// Eliminate values already used in this row
for(int j=0; j<SIZE; j++) {
int value = board[row*SIZE + j].value;
if(value)
board[row*SIZE + col].possVals[value-1] = 0;
}
// Eliminate values already used in this column
for(int i=0; i<SIZE; i++) {
int value = board[i*SIZE + col].value;
if(value)
board[row*SIZE + col].possVals[value-1] = 0;
}
// Eliminate values already used in this square
int squareRow = (row/ROOT_SIZE)*ROOT_SIZE; // Top row of this sub-square
int squareCol = (col/ROOT_SIZE)*ROOT_SIZE; // Left col of this sub-square
for(int i=squareRow; i<squareRow+ROOT_SIZE; i++) {
for(int j=squareCol; j<squareCol+ROOT_SIZE; j++) {
int value = board[i*SIZE + j].value;
if(value)
board[row*SIZE + col].possVals[value-1] = 0;
}
}
}
// Backtracking algorithm implementation
int move(cell* board) {
int row = 0;
int col = 0;
int chosenCell = chooseMove(board, &row, &col);
if(chosenCell == -1)
return -1; // Failed. Were open cells, but no possible values for them
if(chosenCell == 0)
return 0; // Success. No open cells left.
// Array to track changes due to this move. Store index (row*SIZE+col) of cell for which
// possVals was adjusted, for potentially all cells in row, col, and square (3*SIZE)
int adjustments[3*SIZE+1];
// Iterate over, and make move for, each possible values for this cell
for(int i=0; i<SIZE; i++) {
if(board[row*SIZE + col].possVals[i]) {
applyMove(board, adjustments, row, col, i+1);
if(!move(board))
return 0; // Success
else
undoMove(board, adjustments, row, col, i+1); // Undo, so we can try next value
}
}
return -1; //Failed. None of possible values for this cell were sucessful.
}
int chooseMove(cell* board, int* row, int* col) {
*row = -1; // Row of open square with fewest possibilites
*col = -1; // Column of open square with fewest possibilities
int minPoss = SIZE+1; // Number of possible values for cell with fewest
// Find cell with fewest possible values
for(int i=0; i<SIZE; i++) {
for(int j=0; j<SIZE; j++) {
cell* c = board + i*SIZE + j;
// If cell does not yet have a value
if(!c->value) {
// Count possible values for cell
int possibilities = 0;
for(int k=0; k<SIZE; k++) {
if(c->possVals[k])
possibilities++;
}
// Check for open square without possible value
if(!possibilities) {
return -1; // This is a failed branch. Need to backtrack.
}
if(possibilities < minPoss) {
*row = i;
*col = j;
minPoss = possibilities;
}
}
}
}
if(*row == -1)
return 0; // Success. No open squares remaining
else
return 1; // Use board[row, col] for next move
}
int applyMove(cell* board, int* adjustments, int row, int col, int value) {
// Apply value to cell
board[row*SIZE + col].value = value;
int changeNumber = 0;
// Update possibilites for remaining cells
// In row...
for(int j=0; j<SIZE; j++) {
//int value = board[row*SIZE + j].value;
if(!board[row*SIZE + j].value) {
int* possibility = &(board[row*SIZE + j].possVals[value-1]);
if(*possibility) {
*possibility = 0; // This is no longer a possibility
adjustments[changeNumber++] = row*SIZE+j; // Add it to list of adjustmemts
}
}
}
// ... in this column ...
for(int i=0; i<SIZE; i++) {
//int value = board[i*SIZE + col].value;
if(!board[i*SIZE + col].value) {
int* possibility = &(board[i*SIZE + col].possVals[value-1]);
if(*possibility) {
*possibility = 0;
adjustments[changeNumber++] = i*SIZE+col;
}
}
}
// ... and in this square.
int squareRow = (row/ROOT_SIZE)*ROOT_SIZE; // Top row of this sub-square
int squareCol = (col/ROOT_SIZE)*ROOT_SIZE; // Left col of this sub-square
for(int i=squareRow; i<squareRow+ROOT_SIZE; i++) {
for(int j=squareCol; j<squareCol+ROOT_SIZE; j++) {
//int value = board[i*SIZE + j].value;
if(!board[i*SIZE + j].value) {
int* possibility = &(board[i*SIZE + j].possVals[value-1]);
if(*possibility) {
*possibility = 0;
adjustments[changeNumber++] = i*SIZE+j;
}
}
}
}
adjustments[changeNumber] = -1; // Signal end of adjustments
return 0;
}
int undoMove(cell* board, int* adjustments, int row, int col, int value) {
board[row*SIZE + col].value = 0;
// Undo adjustments
int changeNumber = 0;
while(adjustments[changeNumber] != -1) {
board[adjustments[changeNumber++]].possVals[value-1] = value;
}
return 0;
}
void printBoard(cell* board) {
char boardFormat[] = "%i | %i | %i || %i | %i | %i || %i | %i | %i\n";
for(int i=0; i<SIZE; i++) {
printf(boardFormat,
board[i*SIZE].value,
board[i*SIZE + 1].value,
board[i*SIZE + 2].value,
board[i*SIZE + 3].value,
board[i*SIZE + 4].value,
board[i*SIZE + 5].value,
board[i*SIZE + 6].value,
board[i*SIZE + 7].value,
board[i*SIZE + 8].value);
if(i==2 || i==5)
printf("===================================\n");
else if(i != 8)
printf("--+---+---++---+---+---++---+---+--\n");
}
}