-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.c
267 lines (218 loc) · 7.56 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
#include "common.h"
#ifdef KS_SUDOKU_DEBUG
#include <stdio.h>
#endif
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include "sudoku_solver.h"
#include "naked_single_queue.h"
#include "solver_helpers.h"
/**
* Idenitfy the "naked doubles" in a group (row, column or square) and update
* the corresponding group to remove them as possibilities.
* This might reveal other possibilities such as "naked singles", "hidden signles"
* or even other "naked doubles".
*/
static bool solve_naked_doubles(unsigned sudoku_table[TABLE_ORDER_MAX][TABLE_ORDER_MAX],
struct possible_entries possible_values[TABLE_ORDER_MAX][TABLE_ORDER_MAX])
{
bool found_naked_double = false;
// search for naked doubles in rows
#ifdef KS_SUDOKU_DEBUG_NAKED_DOUBLE_SEARCH
printf("solve_naked_doubles: searching for naked doubles in rows\n");
#endif
for (size_t row=0; row<TABLE_ORDER_MAX; row++)
{
found_naked_double |= solve_naked_doubles_helper(sudoku_table, possible_values,
row, row,
0, TABLE_ORDER_MAX-1);
}
// search for naked doubles in cols
#ifdef KS_SUDOKU_DEBUG_NAKED_DOUBLE_SEARCH
printf("solve_naked_doubles: searching for naked doubles in cols\n");
#endif
for (size_t col=0; col<TABLE_ORDER_MAX; col++)
{
found_naked_double |= solve_naked_doubles_helper(sudoku_table, possible_values,
0, TABLE_ORDER_MAX-1,
col, col);
}
#ifdef KS_SUDOKU_DEBUG_NAKED_DOUBLE_SEARCH
printf("solve_naked_doubles: searching for naked doubles in squares\n");
#endif
// search for naked doubles in squares
for (size_t row=0; row<TABLE_ORDER_MAX; row+=SQUARE_DIMENSION)
{
for (size_t col=0; col<TABLE_ORDER_MAX; col+=SQUARE_DIMENSION)
{
found_naked_double |= solve_naked_doubles_helper(sudoku_table, possible_values,
row, row+SQUARE_DIMENSION-1,
col, col+SQUARE_DIMENSION-1);
}
}
return found_naked_double;
}
/**
* Identify the "hidden singles" and solve (fill in) them.
* This might help in identifying other possibilities such as "naked singles",
* "naked doubles" or even other "hidden singles".
*
* This is done by identifying values which occur only once in a group (row, column or square)
* and filling that cell with that value and correspindingly updating the possibilities.
*/
static bool solve_hidden_singles(unsigned sudoku_table[TABLE_ORDER_MAX][TABLE_ORDER_MAX],
struct possible_entries possible_values[TABLE_ORDER_MAX][TABLE_ORDER_MAX])
{
bool found_hidden_single = false;
for (unsigned val=MIN_VALUE; val<=MAX_VALUE; val++)
{
// search for hidden singles in rows
#ifdef KS_SUDOKU_DEBUG_HIDDEN_SINGLE_SEARCH
printf("solve_hidden_singles: searching for hidden singles in rows for val: %u\n", val);
#endif
for (size_t row=0; row<TABLE_ORDER_MAX; row++)
{
found_hidden_single |= solve_hidden_singles_helper(sudoku_table, possible_values,
row, row,
0, TABLE_ORDER_MAX-1,
val);
}
// search for hidden singles in cols
#ifdef KS_SUDOKU_DEBUG_HIDDEN_SINGLE_SEARCH
printf("solve_hidden_singles: searching for hidden singles in cols for val: %u\n", val);
#endif
for (size_t col=0; col<TABLE_ORDER_MAX; col++)
{
found_hidden_single |= solve_hidden_singles_helper(sudoku_table, possible_values,
0, TABLE_ORDER_MAX-1,
col, col,
val);
}
// search for hidden singles in squares
#ifdef KS_SUDOKU_DEBUG_HIDDEN_SINGLE_SEARCH
printf("solve_hidden_singles: searching for hidden singles in squares for val: %u\n", val);
#endif
for (size_t row=0; row<TABLE_ORDER_MAX; row+=SQUARE_DIMENSION)
{
for (size_t col=0; col<TABLE_ORDER_MAX; col+=SQUARE_DIMENSION)
{
found_hidden_single |= solve_hidden_singles_helper(sudoku_table, possible_values,
row, row+SQUARE_DIMENSION-1,
col, col+SQUARE_DIMENSION-1,
val);
}
}
}
#ifdef KS_SUDOKU_DEBUG
printf("\n");
#endif
return found_hidden_single;
}
/**
* Solve (fill in) the "naked single" possibilities in the sudoku table.
* The 'possible_entries' table should be initialized for the given 'sudoku_table'.
* The moves are obtained from the tail-queue whose head is 'naked_singles_head'.
*/
static void solve_naked_singles(unsigned sudoku_table[TABLE_ORDER_MAX][TABLE_ORDER_MAX],
struct possible_entries possible_values[TABLE_ORDER_MAX][TABLE_ORDER_MAX])
{
while (is_naked_single_available())
{
struct naked_single *curr = get_first_naked_single();
remove_first_naked_single();
unsigned naked_single = find_naked_single(possible_values, curr->row, curr->col);
sudoku_table[curr->row][curr->col] = naked_single;
#ifdef KS_SUDOKU_DEBUG
printf("solve_naked_singles: only possibility %u for row: %zu, col: %zu\n",
naked_single, curr->row, curr->col);
#endif
update_possibilities(sudoku_table, possible_values, curr->row, curr->col, naked_single);
#ifdef KS_SUDOKU_DEBUG
printf("solve_naked_singles: Naked single possibilities:\n");
print_naked_singles();
printf("\n");
#endif
free(curr);
}
}
static void solve(unsigned sudoku_table[TABLE_ORDER_MAX][TABLE_ORDER_MAX],
struct possible_entries possible_values[TABLE_ORDER_MAX][TABLE_ORDER_MAX])
{
bool try_next_round = false;
unsigned round = 1;
static const unsigned round_limit = 100;
do
{
solve_naked_singles(sudoku_table, possible_values);
#ifdef KS_SUDOKU_DEBUG
printf("solve: Possibility vectors after trying to solve 'naked singles' in round: %u:\n",
round);
print_possibility_vector(sudoku_table, possible_values);
#endif
bool found_hidden_single = solve_hidden_singles(sudoku_table, possible_values);
#ifdef KS_SUDOKU_DEBUG
if (found_hidden_single)
{
printf("solve: Possibility vectors after trying to solve 'hidden singles' in round: %u:\n",
round);
print_possibility_vector(sudoku_table, possible_values);
}
else
{
printf("solve: No 'hidden single' found in round: %u.\n", round);
}
#endif
bool found_naked_double = solve_naked_doubles(sudoku_table, possible_values);
#ifdef KS_SUDOKU_DEBUG
if (found_naked_double)
{
printf("solve: Possibility vectors after trying to solve 'naked doubles' in round: %u:\n", round);
print_possibility_vector(sudoku_table, possible_values);
}
else
{
printf("solve: No 'naked double' found in round: %u.\n", round);
}
#endif
try_next_round = found_hidden_single | found_naked_double;
round++;
} while (try_next_round == true &&
(round <= round_limit)); // to avoid infinite looping due to repeated detection of "naked doubles"
}
void solve_sudoku(unsigned sudoku_table[TABLE_ORDER_MAX][TABLE_ORDER_MAX])
{
// the lookup table used to identify the possibilities of different cells
struct possible_entries possible_values[TABLE_ORDER_MAX][TABLE_ORDER_MAX];
initialise_naked_single_queue();
#ifdef KS_SUDOKU_DEBUG
printf("\n");
printf("solve_sudoku: 'sudoku_table' obtained as input\n");
print_table(sudoku_table);
#endif
for (size_t row=0; row<TABLE_ORDER_MAX; row++)
{
for (size_t col=0; col<TABLE_ORDER_MAX; col++)
{
if (sudoku_table[row][col] == 0)
{
// initialise the possible values
initialise_possible_values(sudoku_table, possible_values, row, col);
// This could also be done in 'initialise_possible_values_helper'.
// But doing this here saves us some unwanted checking.
if (possible_values[row][col].possibilities == 1)
{
insert_naked_single(row, col);
}
}
}
}
#ifdef KS_SUDOKU_DEBUG
printf("solve_sudoku: Possibility vector after initialization:\n");
print_possibility_vector(sudoku_table, possible_values);
printf("solve_sudoku: Naked single possibilities:\n");
print_naked_singles();
printf("\n");
#endif
solve(sudoku_table, possible_values);
}