-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.cpp
More file actions
576 lines (528 loc) · 17 KB
/
solver.cpp
File metadata and controls
576 lines (528 loc) · 17 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include <boost/unordered_map.hpp>
#include <boost/unordered_set.hpp>
#include <cstring>
#include <iostream>
#include <vector>
#include <utility>
#include "sudoku.h"
using namespace std;
typedef boost::unordered_map<int, cellset> symmap;
template <typename T>
struct reversemap {
typedef boost::unordered_map<T, symmap> t;
};
enum grouptype {
NONE,
ROW,
COL,
BLK
};
template <typename InputIterator>
void print_container(InputIterator first, InputIterator last) {
cout << '{';
for (; first != last; ++first)
cout << *first << ',';
cout << '}' << endl;
}
template <typename T>
bool erase_all(boost::unordered_set<T> &s1,
const boost::unordered_set<T> &s2) {
bool erased = false;
for (typename boost::unordered_set<T>::const_iterator it = s2.begin();
it != s2.end(); ++it)
if (s1.erase(*it) > 0)
erased = true;
return erased;
}
template <typename T>
bool subsetof(const boost::unordered_set<T> &s1,
const boost::unordered_set<T> &s2) {
for (typename boost::unordered_set<T>::const_iterator it = s1.begin();
it != s1.end(); ++it)
if (s2.find(*it) == s2.end())
return false;
return true;
}
// ---------------------------------------------------------------------------
// -------------------------------- AC3 --------------------------------------
// ---------------------------------------------------------------------------
bool ArcReduce(Sudoku &board, const cell &c, bool *error) {
bool change = false;
symset &dom = board[c];
const vector<cell> &conf = board.conflicting(c);
for (vector<cell>::const_iterator it = conf.begin();
it != conf.end(); ++it) {
const symset &dom2 = board[*it];
if (dom2.size() == 1) {
if (dom.erase(*dom2.begin()) > 0)
change = true;
}
}
if (dom.size() == 0)
*error = true;
return change;
}
bool AC3(Sudoku &board) {
cellset todo;
for (int i = 0; i < board.length(); i++) {
for (int j = 0; j < board.length(); j++) {
if (board[i][j].size() == 0)
return false;
todo.insert(cell(i, j));
}
}
while (!todo.empty()) {
cell c = *todo.begin();
todo.erase(todo.begin());
bool error = false;
bool change = ArcReduce(board, c, &error);
if (error)
return false;
if (change) {
const vector<cell> &conf = board.conflicting(c);
todo.insert(conf.begin(), conf.end());
}
}
return true;
}
// ---------------------------------------------------------------------------
// ----------------------------- Swordfish -----------------------------------
// ---------------------------------------------------------------------------
template <typename T>
pair<T, T> set2_to_pair(const boost::unordered_set<T> &set) {
pair<T, T> p;
for (typename boost::unordered_set<T>::const_iterator it = set.begin();
it != set.end(); ++it) {
p.first = *it;
++it;
p.second = *it;
}
return p;
}
template <grouptype TYPE>
bool CheckSwordfish(Sudoku &board,
const boost::unordered_set<pair<cell, cell> > &corners);
// Check this set of cells for a swordfish
// Want exactly two indices for column as well
template <>
bool CheckSwordfish<ROW>(Sudoku &board,
const boost::unordered_set<pair<cell, cell> > &corners) {
bool change = false;
boost::unordered_set<unsigned int> safeI;
boost::unordered_map<unsigned int, int> foundJ;
for (boost::unordered_set<pair<cell, cell> >::const_iterator it = corners.begin();
it != corners.end(); ++it) {
foundJ[it->second.j]++;
// TODO
}
return change;
}
// Check this set of cells for a swordfish
// Want exactly two indices for row as well
template <>
bool CheckSwordfish<COL>(Sudoku &board,
const boost::unordered_set<pair<cell, cell> > &corners) {
bool change = false;
return change;
}
template <grouptype TYPE>
bool SwordfishGroup(Sudoku &board, const reversemap<unsigned int>::t &grpmap) {
bool change = false;
symset done;
for (reversemap<unsigned int>::t::const_iterator it = grpmap.begin();
it != grpmap.end(); ++it) {
//unsigned int x = it->first;
const symmap &smap = it->second;
for (symmap::const_iterator it2 = smap.begin(); it2 != smap.end(); ++it2) {
int sym = it2->first;
if (done.find(sym) != done.end())
continue;
done.insert(sym);
const cellset &cells = it2->second;
if (cells.size() != 2)
continue;
// Find the rest of the groups where this symbol appears twice
boost::unordered_set<pair<cell, cell> > corners;
corners.insert(set2_to_pair(cells));
reversemap<unsigned int>::t::const_iterator it3 = it;
for (++it3; it3 != grpmap.end(); ++it3) {
symmap::const_iterator map_it = it->second.find(sym);
if (map_it != it->second.end() && map_it->second.size() == 2)
corners.insert(set2_to_pair(map_it->second));
}
change |= CheckSwordfish<TYPE>(board, corners);
}
}
return change;
}
bool Swordfish(Sudoku &board, const reversemap<unsigned int>::t &rowmap,
const reversemap<unsigned int>::t &colmap) {
bool change = false;
change |= SwordfishGroup<ROW>(board, rowmap);
change |= SwordfishGroup<COL>(board, colmap);
return change;
}
// ---------------------------------------------------------------------------
// --------------------------- Symbol Removal --------------------------------
// ---------------------------------------------------------------------------
struct groups {
unsigned int row;
unsigned int col;
cell blk;
};
groups SameGroup(const Sudoku &board, const cellset &cells,
bool *row, bool *col, bool *blk) {
groups g;
if (cells.size() == 0) {
*row = *col = *blk = false;
return g;
}
cellset::const_iterator it = cells.begin();
cell c = *it;
g.row = c.i;
g.col = c.j;
g.blk = board.corner(c);
*row = *col = *blk = true;
for (++it; it != cells.end(); ++it) {
cell c2 = *it;
if (g.row != c2.i)
*row = false;
if (g.col != c2.j)
*col = false;
if (g.blk != board.corner(c2))
*blk = false;
}
return g;
}
// Removes the symbols 'syms' from all other cells in the same
// group as 'cells'.
// If the symbols have already been removed from a group, pass ROW, COL,
// or BLK as appropriate as 'type'. Otherwise, pass NONE.
bool RemoveSymsFromOtherCells(Sudoku &board, const cellset &cells,
const symset &syms, grouptype done) {
bool change = false;
bool row, col, blk;
groups g = SameGroup(board, cells, &row, &col, &blk);
if (row && done != ROW) {
for (int j = 0; j < board.length(); j++) {
if (cells.find(cell(g.row, j)) == cells.end())
change |= erase_all(board[g.row][j], syms);
}
}
if (col && done != COL) {
for (int i = 0; i < board.length(); i++) {
if (cells.find(cell(i, g.col)) == cells.end())
change |= erase_all(board[i][g.col], syms);
}
}
if (blk && done != BLK) {
ITERBLOCK(i, j, board, g.blk) {
if (cells.find(cell(i, j)) == cells.end())
change |= erase_all(board[i][j], syms);
}
}
return change;
}
// ---------------------------------------------------------------------------
// ------------------------- Naked Permutations ------------------------------
// ---------------------------------------------------------------------------
// Checks to see whether cell 'c' with domain 'dom' is a superset of a
// naked permutation in a group (which is given by 'TYPE').
template <grouptype TYPE>
bool SearchGroupForNaked(Sudoku &board, cellset &done, const symset &dom,
const cell &c) {
if (done.find(c) == done.end()) {
cellset found;
found.insert(c);
for (int x = 0; x < board.length(); x++) {
cell c2 (c);
if (TYPE == ROW)
c2.j = x;
else if (TYPE == COL)
c2.i = x;
if (board[c2].size() == 1)
continue;
if (c2 != c && done.find(c2) == done.end() &&
subsetof(board[c2], dom)) {
found.insert(c2);
}
}
if (found.size() == dom.size()) {
done.insert(found.begin(), found.end());
return RemoveSymsFromOtherCells(board, found, dom, NONE);
}
}
return false;
}
// Checks to see whether cell 'c' with domain 'dom' is a superset of a
// naked permutation in a group (which is given by 'TYPE').
template <>
bool SearchGroupForNaked<BLK>(Sudoku &board, cellset &done, const symset &dom,
const cell &c) {
if (done.find(c) == done.end()) {
done.insert(c);
cellset found;
found.insert(c);
cell cnr = board.corner(c);
ITERBLOCK(i2, j2, board, cnr) {
cell c2 (i2, j2);
if (board[c2].size() == 1)
continue;
if (c2 != c && done.find(c2) == done.end() &&
subsetof(board[c2], dom)) {
found.insert(c2);
}
}
if (found.size() == dom.size()) {
done.insert(found.begin(), found.end());
return RemoveSymsFromOtherCells(board, found, dom, NONE);
}
}
return false;
}
/**
* for each cell c of size k:
* find other cells c' with D(c) = D(c')
* if k cells total, naked exact perm
*
* Misses perms like (2,3),(3,4),(2,4)
* Catches (2,4),(2,4) or (2,3,4),(2,3,4),(2,3,4) or (2,3,4),(2,3),(3,4)
*/
bool FindMostNakedPerms(Sudoku &board, unsigned int max_perm_size) {
bool change = false;
// This keeps track of whether a cell needs to be searched for perms
// in its row, column, or block.
cellset doneR;
cellset doneC;
cellset doneB;
const vector<cell> &cells = board.OrderedCells();
for (vector<cell>::const_iterator it = cells.begin();
it != cells.end(); ++it) {
cell c = *it;
const symset &dom = board[c];
int k = dom.size();
if (k == 1 || k > max_perm_size)
continue;
change |= SearchGroupForNaked<ROW>(board, doneR, dom, c);
change |= SearchGroupForNaked<COL>(board, doneC, dom, c);
change |= SearchGroupForNaked<BLK>(board, doneB, dom, c);
}
return change;
}
// ---------------------------------------------------------------------------
// ------------------------- Hidden Permutations -----------------------------
// ---------------------------------------------------------------------------
void MakeReverseMaps(const Sudoku &board,
reversemap<unsigned int>::t &rowmap,
reversemap<unsigned int>::t &colmap,
reversemap<cell>::t &blkmap) {
for (int i = 0; i < board.length(); i++) {
symmap &mapR = rowmap[i];
for (int j = 0; j < board.length(); j++) {
symmap &mapC = colmap[j];
symmap &mapB = blkmap[board.corner(i, j)];
const symset &dom = board[i][j];
if (dom.size() == 1)
continue;
cell c = cell(i, j);
for (symset::const_iterator it = dom.begin(); it != dom.end(); ++it) {
mapR[*it].insert(c);
mapC[*it].insert(c);
mapB[*it].insert(c);
}
}
}
}
// Delete all but the symbols in 'syms' from the cells in 'perm'.
bool ProcessHiddenPerm(Sudoku &board, const cellset &perm,
const symset &syms) {
bool change = false;
for (cellset::const_iterator it = perm.begin(); it != perm.end(); ++it) {
symset &dom = board[*it];
symset::iterator it2 = dom.begin();
while (it2 != dom.end()) {
if (syms.find(*it2) == syms.end()) {
it2 = dom.erase(it2);
change = true;
}
else
++it2;
}
}
return change;
}
template <grouptype T>
struct MapKey {
typedef unsigned int key;
};
template <>
struct MapKey<BLK> {
typedef cell key;
};
template <grouptype TYPE>
struct Map {
typedef typename reversemap<typename MapKey<TYPE>::key>::t t;
};
template <grouptype TYPE>
void FindOtherSyms(const Sudoku &board, const cellset &cells,
symset &others, typename MapKey<TYPE>::key x);
template <>
void FindOtherSyms<ROW>(const Sudoku &board, const cellset &cells,
symset &others, MapKey<ROW>::key i) {
for (int j = 0; j < board.length(); j++) {
if (cells.find(cell(i, j)) == cells.end())
others.insert(board[i][j].begin(), board[i][j].end());
}
}
template <>
void FindOtherSyms<COL>(const Sudoku &board, const cellset &cells,
symset &others, MapKey<COL>::key j) {
for (int i = 0; i < board.length(); i++) {
if (cells.find(cell(i, j)) == cells.end())
others.insert(board[i][j].begin(), board[i][j].end());
}
}
template <>
void FindOtherSyms<BLK>(const Sudoku &board, const cellset &cells,
symset &others, MapKey<BLK>::key c) {
ITERBLOCK(i, j, board, c) {
if (cells.find(cell(i, j)) == cells.end())
others.insert(board[i][j].begin(), board[i][j].end());
}
}
/**
* for each sym s:
* find c_1,...,c_k s.t. s in D(c)
* if union of c_i minus the rest of the group has size k:
* we have a hidden perm
*
* This will catch the case where a symbol can only go in one cell
*/
template <grouptype TYPE>
bool SearchGroupForHidden(Sudoku &board, unsigned int max_perm_size,
const typename Map<TYPE>::t &grpmap) {
bool change = false;
for (typename Map<TYPE>::t::const_iterator it = grpmap.begin();
it != grpmap.end(); ++it) {
typename MapKey<TYPE>::key x = it->first;
const symmap &smap = it->second;
for (symmap::const_iterator it2 = smap.begin(); it2 != smap.end(); ++it2) {
int sym = it2->first;
const cellset &cells = it2->second;
int k = cells.size();
if (k <= board.blocksize()) {
symset singleton;
singleton.insert(sym);
change |= RemoveSymsFromOtherCells(board, cells, singleton, TYPE);
}
if (k > max_perm_size)
continue;
// (union of cells) \ (union of not cells)
// if that size is k, we're in business
symset others;
FindOtherSyms<TYPE>(board, cells, others, x);
symset these;
for (cellset::const_iterator it3 = cells.begin();
it3 != cells.end(); ++it3)
these.insert(board[*it3].begin(), board[*it3].end());
erase_all(these, others);
if (these.size() == k) {
// delete everything from cells not in these
// check whether naked perm in other group
change |= ProcessHiddenPerm(board, cells, these);
}
}
}
return change;
}
// Looks for hidden permutations and swordfish. These are in the same
// function because they both make use of the symbol maps.
bool HiddenAndSwordfish(Sudoku &board, unsigned int max_perm_size) {
bool change = false;
reversemap<unsigned int>::t rowmap;
reversemap<unsigned int>::t colmap;
reversemap<cell>::t blkmap;
MakeReverseMaps(board, rowmap, colmap, blkmap);
change |= SearchGroupForHidden<ROW>(board, max_perm_size, rowmap);
change |= SearchGroupForHidden<COL>(board, max_perm_size, colmap);
change |= SearchGroupForHidden<BLK>(board, max_perm_size, blkmap);
//change |= Swordfish(board, rowmap, colmap);
return change;
}
// ---------------------------------------------------------------------------
// ------------------------------ Solvers ------------------------------------
// ---------------------------------------------------------------------------
// TODO nested while loops, common strategies in the inner one
bool LogicSolve(Sudoku &board) {
bool change = true;
bool success = AC3(board);
unsigned int max_perm_size = board.blocksize();
while (change) {
if (!success)
return false;
bool res1 = HiddenAndSwordfish(board, max_perm_size);
if (res1)
success &= AC3(board);
bool res2 = FindMostNakedPerms(board, max_perm_size);
if (res2)
success &= AC3(board);
success &= AC3(board);
change = res1 || res2;
}
return true;
}
bool GuessSolve(Sudoku &board) {
bool success = LogicSolve(board);
if (!success || board.Solved())
return success;
// TODO smarter guess?
cell guess = board.OrderedCells().front();
for (symset::const_iterator it = board[guess].begin();
it != board[guess].end(); ++it) {
Sudoku board2 = board.Clone();
symset &dom = board2[guess];
dom.clear();
dom.insert(*it);
if (GuessSolve(board2)) {
board = board2;
return true;
}
}
return false;
}
void print_usage() {
cout << "Sudoku Solver\n" << endl;
cout << "solver [--logic] puzzle\n" << endl;
cout << "Solves the Sudoku puzzle, guessing if necessary. If the --logic\n";
cout << "flag is provided, the solver will only use logic to try to solve\n";
cout << "the puzzle, though it may be unable to completely solve it.";
cout << endl;
exit(0);
}
bool process_args(int argc, char ***argv) {
char **args = *argv;
bool logic = false;
if (argc > 3 || argc < 2)
print_usage();
for (int i = 1; i < argc; i++) {
if (strncmp(args[i], "--logic", 7) == 0)
logic = true;
else
*argv = args + i;
}
if (logic + argc == 3)
print_usage();
return logic;
}
int main(int argc, char **argv) {
bool logic = process_args(argc, &argv);
cout << argv[0] << endl;
Sudoku s = Sudoku::ParseFromFile(argv[0]);
cout << s.ToString() << endl;
if (logic)
LogicSolve(s);
else
GuessSolve(s);
cout << s.ToString() << endl;
cout << (s.Solved() ? "Solved!" : "Unsolved") << endl;
}