This repository has been archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolving_algorithms.cpp
470 lines (409 loc) · 15.3 KB
/
solving_algorithms.cpp
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include "solving_algorithms.h"
using namespace std;
/*
* Checks if cell has a single candidate. If it does, solve_cell().
*
* Returns 'true' if a cell has been changed
*/
bool single_candidate(array< array<Cell, 9>, 9>&board, int x, int y) {
/*
if ((board[x][y].candidates.count() == 0) && (board[x][y].val == -1)) {
// No candidates and space is empty - unsolvable!
cout << "WE HAVE HOPE" << endl;
cout << endl << "Board is unsolvable :(" << endl;
print_board(board);
exit(EXIT_FAILURE);
}
*/
//else if (board[x][y].candidates.count() == 1) {
if (board[x][y].candidates.count() == 1) {
// if only 1 bit is set, cell is solvable!
cout << "single candidate:" << endl;
solve_cell(board, x, y);
return true;
}
return false;
} /* single_candidate() */
/* #############################
* # UNIQUE VALUE CHECKERS #
* #############################
*/
/*
* These functions check the Cell's row/col/sector to find out if the Cell
* has a candidate that is unique to its row/col/sector. For example:
* Cell2 candidates 2, 3, 4, 7, 8, 9: 1 1 1 0 0 1 1 1 0
* Cell3 candidates 6, 7, 9 : 1 0 1 1 0 0 0 0 0 OR
* Cell4 candidates 2, 3, 4, 6, 7, 9: 1 0 1 1 0 1 1 1 0
* etc... -------------------
* 1 1 1 1 0 1 1 1 0 XOR
* Cell candidates 1, 3, 7, 8, 9 : 1 1 1 0 0 0 1 0 1
* -------------------
* 0 0 0 1 0 1 0 1 1 AND
* Cell candidates 1, 3, 7, 8, 9 : 1 1 1 0 0 0 1 0 1
* -------------------
* 0 0 0 0 0 0 0 0 1
* So the Cell has a unique candidate of 1 in its row/col/sector, therefore
* its value must be 1.
*/
/*
* Checks if cell has a unique candidate for the row that it's in. If it does,
* solve_cell().
*
* Returns 'true' if a cell has been changed.
*/
bool unique_in_row(array< array<Cell, 9>, 9>&board, int x, int y) {
bitset<9> unique_tester = { 0 };
for (int i = 0; i < 9; i++) {
// OR each Cell's candidates field to get all present candidates in row
if (y != i) {
unique_tester |= board[x][i].candidates;
}
}
unique_tester ^= board[x][y].candidates;
unique_tester &= board[x][y].candidates;
if (unique_tester.count() == 1) {
// set Cell's candidates to new bitset if there is a unique value in its row
board[x][y].candidates = unique_tester;
cout << "unique in row:" << endl;
solve_cell(board, x, y);
return true;
}
return false;
} /* unique_in_row() */
/*
* Checks if cell has a unique candidate for the col that it's in. If it does,
* solve_cell().
*
* Returns 'true' if a cell has been changed.
*/
bool unique_in_col(array< array<Cell, 9>, 9>&board, int x, int y) {
bitset<9> unique_tester = { 0 };
for (int i = 0; i < 9; i++) {
// OR each Cell's candidates field to get all present candidates in col
if (x != i) {
unique_tester |= board[i][y].candidates;
}
}
unique_tester ^= board[x][y].candidates;
unique_tester &= board[x][y].candidates;
if (unique_tester.count() == 1) {
// set Cell's candidates to new bitset if there is a unique value in its col
board[x][y].candidates = unique_tester;
cout << "unique in col:" << endl;
solve_cell(board, x, y);
return true;
}
return false;
} /* unique_in_col() */
/*
* Checks if cell has a unique candidate for the sector that it's in. If it
* does, solve_cell().
*
* Returns 'true' if a cell has been changed.
*/
bool unique_in_sector(array< array<Cell, 9>, 9>&board, int x, int y) {
array<array<int, 2>, 9> coords = get_sector_coords(get_sector(x, y));
bitset<9> unique_tester = { 0 };
int a = 0;
int b = 0;
for (int i = 0; i < 9; i++) {
a = coords[i][0];
b = coords[i][1];
if ((a != x) || (b != y)) {
// OR each Cell's candidates field to get all present candidates in sector
unique_tester |= board[a][b].candidates;
}
}
unique_tester ^= board[x][y].candidates;
unique_tester &= board[x][y].candidates;
if (unique_tester.count() == 1) {
// set Cell's candidates to new bitset if there is a unique value in its col
board[x][y].candidates = unique_tester;
cout << "unique in sector:" << endl;
solve_cell(board, x, y);
return true;
}
return false;
} /* unique_in_sector() */
/*
* If 2 Cells in the same row/col only have 2 options (the same 2 options),
* AND are in the same sector, those 2 Cells are the place where those options
* must lie.
*/
bool unique_pair_in_sector(array< array<Cell, 9>, 9>&board, int x, int y) {
if (board[x][y].narrowed_down) {
return false;
}
array<array<int, 2>, 9> coords = get_sector_coords(get_sector(x, y));
int a = 0;
int b = 0;
for (int i = 0; i < 9; i++) {
a = coords[i][0];
b = coords[i][1];
if ((a != x) || (b != y)) {
if ((board[a][b].candidates == board[x][y].candidates) &&
(board[a][b].candidates.count() == 2)) {
bitset<9> temp_candidates = board[x][y].candidates;
// extract candidates from bitset
int candidate[2] = {0, 0};
int index = 0;
for (int j = 8; j >= 0; j--) {
if (board[a][b].candidates[j] == 1) {
candidate[index] = j + 1;
index++;
}
}
if (a == x) {
// Cells are in the same row, so remove those 2 candidates from the row
remove_candidate_row(board, x, y, candidate[0]);
remove_candidate_row(board, x, y, candidate[1]);
}
else if (b == y) {
// Cells are in the same column, so remove those 2 candidates from the col
remove_candidate_col(board, x, y, candidate[0]);
remove_candidate_col(board, x, y, candidate[1]);
}
remove_candidate_sector(board, x, y, candidate[0]);
remove_candidate_sector(board, x, y, candidate[1]);
// adds those candidates back to the 2 Cells since we didn't solve anything
board[x][y].candidates = temp_candidates;
board[a][b].candidates = temp_candidates;
// This field is to prevent an infinite loop from re-calling this
// function on a pair of Cells that have 2 candidates (since no Cell is solved)
board[x][y].narrowed_down = true;
board[a][b].narrowed_down = true;
cout << "unique pair in sector:" << endl;
cout << "(" << a << ", " << b << ") AND (" << x << ", " << y <<
") have unique candidates " << candidate[0] << " and " <<
candidate[1] << endl;
cout << "-----------------------------------------------------" << endl;
return true;
}
}
}
return false;
} /* unique_pair_in_sector() */
/*
* This function exists to minimize repeated code in the common_val_in_sector
* function.
*/
void common_val_in_sector_helper(array< array<Cell, 9>, 9>&board,
vector< vector< array<int, 2> > > combinations,
array< array<int, 2>, 9> coords) {
bitset<9> common_tester_vals = { 0 };
common_tester_vals.set();
bitset<9> other_cell_vals = { 0 };
// used to temporarily store coordinates for each Cell
int a = 0;
int b = 0;
// find common values between Cells that you're comparing
for (int i = 0; i < combinations.size(); i++) {
common_tester_vals.set();
other_cell_vals.reset();
for (int j = 0; j < combinations[i].size(); j++) {
a = combinations[i][j][0];
b = combinations[i][j][1];
common_tester_vals &= board[a][b].candidates;
}
// find all present values in sector (not including Cells you're comparing)
// using bitwise OR operation
int valid_cell = true;
for (int j = 0; j < coords.size(); j++) {
valid_cell = true;
a = coords[j][0];
b = coords[j][1];
for (int k = 0; k < combinations[i].size(); k++) {
valid_cell &= ((combinations[i][k][0] != a) || (combinations[i][k][1] != b));
}
if (valid_cell) {
other_cell_vals |= board[a][b].candidates;
}
}
// XOR, then AND, with common values in Cells you're comparing to see if
// there are any unique values in the Cells you're comparing
other_cell_vals ^= common_tester_vals;
other_cell_vals &= common_tester_vals;
//TODO: might have to be a unique bit set, but I don't think I see any problem with
// having multiple unique bits right now
if (other_cell_vals.count() == 1) {
int candidate = log2(other_cell_vals.to_ulong()) + 1;
//for (int i = 0; i < combinations.size(); i++) {
bool same_row = true;
bool same_col = true;
for (int j = 0; j < combinations[i].size() - 1; j++) {
same_row &= combinations[i][j][0] == combinations[i][j + 1][0];
same_col &= combinations[i][j][1] == combinations[i][j + 1][1];
}
if (same_row) {
// if all tester Cells are in the same row, remove_row
cout << "common val in row --> candidate = " << candidate << endl;
cout << "-----------------------------------------------------" << endl;
remove_candidate_row(board, combinations[i][0][0],
combinations[i][0][1], candidate);
// pt2
remove_candidate_sector(board, combinations[i][0][0],
combinations[i][0][1], candidate);
// add candidates back
for (int j = 0; j < combinations[i].size(); j++) {
a = combinations[i][j][0];
b = combinations[i][j][1];
board[a][b].candidates.set(candidate - 1);
}
}
if (same_col) {
// if all tester Cells are in the same col, remove_col
cout << "common val in col --> candidate = " << candidate << endl;
cout << "-----------------------------------------------------" << endl;
remove_candidate_col(board, combinations[i][0][0],
combinations[i][0][1], candidate);
// pt2
remove_candidate_sector(board, combinations[i][0][0],
combinations[i][0][1], candidate);
// add candidates back
for (int j = 0; j < combinations[i].size(); j++) {
a = combinations[i][j][0];
b = combinations[i][j][1];
board[a][b].candidates.set(candidate - 1);
}
}
}
}
} /* common_val_in_sector_helper() */
/*
* pt1: if there is a pair/triplet of Cells in a sector that are in the same
* row/column, and there is a value that is unique to all of
* those Cells in the sector, that value can be removed from the
* corresponding row/column (bitwise AND the Cells and check
* the rest of the sector for existence. if there are unique values,
* remove them from the corresponding row/col)
* ------------------------------------------------------------------------
* pt2: if there is a pair/triplet of Cells in a sector that are in the same
* row/column, and there is a value in all of those Cells that is
* unique to that row/column, that value can be removed from the
* corresponding sector (bitwise AND the cells and check
* for existence in the corresponding row/col)
*
*/
void common_val_in_sector(array< array<Cell, 9>, 9>&board, int x, int y) {
// pt1
array< array<int, 2>, 9> coords = get_sector_coords(get_sector(x, y));
// find all combinations of 2 coordinates
vector< vector< array<int, 2> > > combinations;
vector< array<int, 2> > combination;
combination_finder(coords, 2, 0, combinations, combination);
common_val_in_sector_helper(board, combinations, coords);
// find all combinations of 3 coordinates
combination_finder(coords, 3, 0, combinations, combination);
common_val_in_sector_helper(board, combinations, coords);
} /* common_val_in_sector() */
/*
* Recursive brute force method. LAST RESORT!! Essentially guesses one Cell and
* tries to solve the rest of the board normally. If stuck, recurses and
* guesses another Cell. If at any point the new board has a Cell that is
* unsolved and has no candidates, that brute force method failed, so break out
* of that branch of recursion.
*/
bool brute_force(array< array<Cell, 9>, 9>&board, int x, int y, int candidate_guess) {
board[x][y].candidates.reset();
board[x][y].candidates.set(candidate_guess - 1);
solve_cell(board, x, y);
if (run_solving_algorithms(board) == false) {
return false;
}
else {
return true;
}
} /* brute_force() */
/*
* Wrapper function to run all solving algorithms on the cell passed in.
*
* common_val_in_sector is hard to keep track of, so call it once before and
* once after the iteration to make surue that it does something when it needs
* to. Prevents premature termination of the program.
*/
bool run_solving_algorithms(array< array<Cell, 9>, 9>&board) {
bool changed = false;
int last_sector = -1;
int curr_sector = -1;
while (1) {
changed = false;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j].val == -1) {
last_sector = curr_sector;
curr_sector = get_sector(i, j);
if (curr_sector != last_sector) {
common_val_in_sector(board, i, j);
}
if (single_candidate(board, i, j) == true) {
changed |= true;
continue;
}
changed |= false;
if (unique_in_row(board, i, j)) {
changed |= true;
continue;
}
changed |= false;
if (unique_in_col(board, i, j)) {
changed |= true;
continue;
}
changed |= false;
if (unique_in_sector(board, i, j)) {
changed |= true;
continue;
}
changed |= false;
//TODO: expand to unique pair in sector/row/col
if (unique_pair_in_sector(board, i, j)) {
changed |= true;
continue;
}
changed |= false;
if (curr_sector != last_sector) {
common_val_in_sector(board, i, j);
}
}
}
}
if (!changed) {
if (!solved(board)) {
// #############################
// # BRUTE FORCE SOLVE #
// #############################
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j].val == -1) {
for (int x = 0; x < 9; x++) {
// make copy of board
array< array<Cell, 9>, 9> board_copy = { 0 };
copy(board.begin(), board.end(), board_copy.begin());
if (board[i][j].candidates[x] == true) {
if (brute_force(board_copy, i, j, x + 1) == true) {
// if brute_force returns true, board is solved
board = board_copy;
return true;
}
}
}
cout << endl << "Board is unsolvable :(" << endl;
return false;
}
}
}
}
cout << " SOLVED BOARD " << endl;
return true;
// print candidates
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cout << i << ", " << j << ": " << board[i][j].candidates.to_string() << endl;
}
}
}
}
} /* run_solving_algorithms() */