-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
333 lines (309 loc) · 8.23 KB
/
sudoku.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
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
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define PUZZLE_ROWS 9
#define PUZZLE_COLS 9
#define PUZZLE_SIZE PUZZLE_ROWS*PUZZLE_COLS
#define ANSII_BLUE_BG "\033[44m"
#define ANSII_RED_BG "\033[41m"
#define ANSII_GREEN_BG "\033[42m"
#define ANSII_CLEAR "\033[0m"
typedef struct sudoku_cell_t
{
char c;
bool predefined;
bool sure;
bool empty;
} sudoku_cell_t;
sudoku_cell_t *get_cell(sudoku_cell_t *puzzle, unsigned int row, unsigned int column)
{
sudoku_cell_t *cell = &puzzle[row * PUZZLE_COLS + column];
return cell;
}
void set_cell(sudoku_cell_t *puzzle, unsigned int row, unsigned int column, bool predefined, bool sure, char c)
{
sudoku_cell_t *cell = get_cell(puzzle, row, column);
cell->predefined = predefined;
cell->sure = predefined ? true : sure;
cell->c = c;
cell->empty = c == 0;
}
void set_cellc(sudoku_cell_t *puzzle, char column, unsigned int row, bool predefined, bool sure, char c)
{
unsigned int irow = row - 1;
unsigned int icol = column - 65;
set_cell(puzzle, irow, icol, predefined, sure, c);
}
void set_predefinedc(sudoku_cell_t *puzzle, char row, unsigned int column, char c)
{
set_cellc(puzzle, row, column, true, true, c);
}
void set_sure(sudoku_cell_t *puzzle, char row, unsigned int column, char c)
{
set_cell(puzzle, row, column, false, true, c);
}
void set_guess(sudoku_cell_t *puzzle, char row, unsigned int column, char c)
{
set_cell(puzzle, row, column, false, false, c);
}
void set_guessc(sudoku_cell_t *puzzle, char row, unsigned int column, char c)
{
set_cellc(puzzle, row, column, false, false, c);
}
void set_zero(sudoku_cell_t *puzzle, char row, unsigned int column)
{
set_cellc(puzzle, row, column, false, false, 0);
}
void init_puzzle(sudoku_cell_t *puzzle, unsigned int rows, unsigned int cols)
{
for (unsigned int i = 0; i < rows * cols; i++)
{
puzzle[i].c = 0;
puzzle[i].empty = true;
puzzle[i].predefined = false;
puzzle[i].sure = true;
}
}
void draw_cell(sudoku_cell_t *cell)
{
if (cell->predefined)
{
printf("%s", ANSII_BLUE_BG);
}
else if (cell->empty)
{
printf("%s", ANSII_CLEAR);
}
else if (cell->sure)
{
printf("%s", ANSII_GREEN_BG);
}
else if (!cell->empty)
{
printf("%s", ANSII_RED_BG);
}
printf(" %c ", cell->c == 0 ? ' ' : cell->c);
printf("%s", ANSII_CLEAR);
}
void draw_puzzle(sudoku_cell_t *puzzle, unsigned int rows, unsigned int cols)
{
for (unsigned int i = 0; i < cols; i++)
{
for (unsigned int j = 0; j < rows; j++)
{
sudoku_cell_t *cell = &puzzle[i * cols + j];
draw_cell(cell);
}
printf("\n");
}
}
sudoku_cell_t *get_col(sudoku_cell_t *puzzle, unsigned int rows, unsigned int col)
{
static sudoku_cell_t box[4096];
for (unsigned int i = 0; i < rows; i++)
{
box[i] = *get_cell(puzzle, i, col);
}
return box;
}
sudoku_cell_t *get_row(sudoku_cell_t *puzzle, unsigned int cols, unsigned int row)
{
static sudoku_cell_t box[4096];
for (unsigned int i = 0; i < cols; i++)
{
box[i] = *get_cell(puzzle, row, i);
}
return box;
}
sudoku_cell_t *get_box(sudoku_cell_t *puzzle, unsigned int rows, unsigned int cols, unsigned int row, unsigned int col)
{
unsigned int col_box_column = col / 3;
unsigned int row_box_row = row / 3;
static sudoku_cell_t box[4096];
unsigned int counter = 0;
for (unsigned int i = row_box_row * (rows / 3); i < (row_box_row * rows / 3) + (rows / 3); i++)
{
for (unsigned int j = col_box_column * (cols / 3); j < (col_box_column * cols / 3) + (cols / 3); j++)
{
box[counter] = *get_cell(puzzle, i, j);
counter++;
}
}
return box;
}
unsigned int *get_options(sudoku_cell_t *puzzle, unsigned int rows, unsigned int cols, unsigned int row, unsigned int col)
{
static unsigned int options[4096];
memset(options, 0, sizeof(options));
for (unsigned int i = 0; i < cols; i++)
{
options[i] = true;
}
for (unsigned int i = 0; i < cols; i++)
{
sudoku_cell_t *c = get_col(puzzle, rows, col);
for (unsigned int j = 0; j < cols; j++)
{
if (!c[j].empty)
options[c[i].c - '0' - 1] = false;
}
c = get_row(puzzle, cols, row);
for (unsigned int j = 0; j < cols; j++)
{
if (!c[j].empty)
options[c[j].c - '0' - 1] = false;
}
c = get_box(puzzle, rows, cols, row, col);
for (unsigned int j = 0; j < cols * cols; j++)
{
if (!c[j].empty)
options[c[j].c - '0' - 1] = false;
}
}
return options;
}
unsigned int get_option(unsigned int *options, unsigned int offset)
{
unsigned int result = 0;
unsigned int current_offset = 0;
for (unsigned int i = 0; i < PUZZLE_COLS; i++)
{
if (options[i])
{
if (current_offset == offset)
{
return i;
}
offset++;
}
}
return result;
}
unsigned int get_options_count(unsigned int *options)
{
unsigned int result = 0;
for (unsigned int i = 0; i < PUZZLE_COLS; i++)
{
if (options[i])
{
result++;
}
}
return result;
}
unsigned int count_empty(sudoku_cell_t * puzzle, unsigned int size){
unsigned int count = 0;
for(unsigned int i = 0; i < size;i++){
if(puzzle[i].empty){
count++;
}
}
return count;
}
unsigned int set_sures(sudoku_cell_t *puzzle, unsigned int rows, unsigned int cols, unsigned int accuracy)
{
for (unsigned int i = 0; i < rows; i++)
{
for (unsigned int j = 0; j < cols; j++)
{
sudoku_cell_t *cell = get_cell(puzzle, i, j);
unsigned int *options = get_options(puzzle, rows, cols, i, j);
if (cell->empty && get_options_count(options) == accuracy)
{
unsigned int option = get_option(options, 0);
if (accuracy > 1)
{
set_guess(puzzle, i, j, option + '0' + 1);
}
else
{
set_sure(puzzle, i, j, option + '0' + 1);
}
set_sures(puzzle, rows, cols, accuracy);
return count_empty(puzzle,rows*cols);
}
}
}
return count_empty(puzzle,rows*cols);
}
unsigned int solve(char *charpuzzle)
{
sudoku_cell_t puzzle[PUZZLE_SIZE];
init_puzzle(puzzle, PUZZLE_ROWS, PUZZLE_COLS);
for (unsigned int i = 0; i < PUZZLE_ROWS; i++)
{
for (unsigned int j = 0; j < PUZZLE_COLS; j++)
{
if (charpuzzle[j * PUZZLE_COLS + i] != ' ')
set_predefinedc(puzzle, i + 65, j + 1, charpuzzle[j * PUZZLE_COLS + i]);
}
}
set_sures(puzzle, PUZZLE_ROWS, PUZZLE_COLS, 1);
unsigned int result = set_sures(puzzle, PUZZLE_ROWS, PUZZLE_COLS, 2);
if(result){
printf("Unresolved. %d cells left:\n",result);
}else{
printf("Succesfully resolved:\n");
}
draw_puzzle(puzzle, PUZZLE_ROWS, PUZZLE_COLS);
return result;
}
int main()
{
solve(
"913 5 "
"6 7 24"
" 5 8 7 "
" 79 "
" 2 9 43"
" 4 9 "
" 4 19 "
"7 6 9 5"
" 1 64 7");
printf("\n");
solve(
"5 3 7 "
"6 195 "
" 98 6 "
"8 6 3"
"4 8 3 "
"7 2 6"
" 6 28 "
" 419 5"
" 8 79");
printf("\n");
solve(
" 4 8 3 "
" 1 9 5"
"7 2 6 "
" 5 7 2"
" 4 "
"9 5 7 "
" 2 6 8"
"6 3 1 "
" 8 7 9 ");
printf("\n");
solve(
" 7429 8 "
"2 83 "
"896 351 4"
" 1 6 9 "
"7 1 2 6"
"6 973 1"
" 6 "
"5 8 9 "
"46 58 1 ");
printf("\n");
solve(
"2 5 74 6"
" 31 "
" 23 "
" 2 "
"86 31 "
" 45 "
" 9 7 "
" 695 2"
" 1 6 8");
printf("\n");
return 0;
}