-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsudokuSolver.cpp
More file actions
444 lines (387 loc) · 11.6 KB
/
Copy pathsudokuSolver.cpp
File metadata and controls
444 lines (387 loc) · 11.6 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
// https://leetcode.com/problems/sudoku-solver/
// Many of these methods come from validSudoku.cpp
//
// Status: Accepted
// Runtime: 84 ms
// Score: - 20.93%
//
#include <iostream>
#include <array>
#include <vector>
#include <gtest/gtest.h>
#include "args.h"
#include "macros.h"
using namespace std;
#define UNSOLVED '.'
struct Cell
{
// Also keyed for N=9 and assuming numeric Sudoku
int n;
char value{UNSOLVED};
};
typedef vector<vector<char>> Chars;
typedef array<array<Cell, 9>, 9> Cells;
class Sudoku
{
public:
// Only solve for N=9, but could expand generically
Sudoku() : _R(3), _C(3), _solved(false)
{
_M = _R * _C;
_N = _M * _M;
}
void display(Cells &board, bool code)
{
cout << (code ? "{" : "");
for (int n = 0; n < _N; n++)
{
int r = n2r(n);
int c = n2c(n);
if (c == 0)
{
cout << (code ? "{" : "");
}
cout << (code ? "'" : "")
<< board[r][c].value
<< (code ? "'," : "")
<< " ";
if (c == (_M - 1))
{
cout << (code ? "}," : "");
cout << endl;
}
}
cout << (code ? "}" : "") << endl;
}
bool getCandidates(Cells &board, int r, int c, int &values)
{
try
{
int b = rc2b(r, c);
int rowValues = 0;
int columnValues = 0;
int boxValues = 0;
for (int i = 0; i < _M; i++)
{
checkRow(r, i, board, rowValues);
checkColumn(i, c, board, columnValues);
checkBox(b, i, board, boxValues);
}
values = (rowValues | columnValues | boxValues);
}
catch (...)
{
return false;
}
return true;
}
void from(Chars &board)
{
for (int n = 0; n < _N; n++)
{
int r = n2r(n);
int c = n2c(n);
char value = board[r][c];
_cells[r][c].n = n;
_cells[r][c].value = value;
if (value == UNSOLVED)
{
_unsolved.push_back(&_cells[r][c]);
}
}
}
void to(Chars &board)
{
for (int n = 0; n < _N; n++)
{
int r = n2r(n);
int c = n2c(n);
board[r][c] = _cells[r][c].value;
}
}
void solve()
{
_cells = solve(_cells, _unsolved);
_solved = true;
}
bool isSolved()
{
return _solved;
}
bool check()
{
vector<Cell *> unsolved = getUnsolved(_cells);
return (unsolved.size() == 0);
}
private:
int n2r(int n)
{
return (n / _M);
}
int n2c(int n)
{
return (n % _M);
}
int rc2b(int r, int c)
{
return (r / _R) * _R + (c / _C);
}
Cells solve(Cells board, vector<Cell *> unsolved)
{
Cells b = board;
for (auto &cell : unsolved)
{
int r = n2r(cell->n);
int c = n2c(cell->n);
int candidates = 0;
if (!getCandidates(b, r, c, candidates))
{
return board;
}
for (int i = 1; i <= _M; i++)
{
if ((candidates & (1 << i)))
{
continue;
}
b[r][c].value = 0x30 + i;
int u = unsolved.size() - 1;
if (u == 0)
{
_solved = true;
}
else
{
vector<Cell *> un(unsolved.begin() + 1, unsolved.end());
b = solve(b, un);
}
if (_solved)
{
return b;
}
}
return board;
}
return board;
}
vector<Cell *> getUnsolved(Cells &board)
{
vector<Cell *> unsolved;
for (int n = 0; n < _N; n++)
{
int r = n2r(n);
int c = n2c(n);
if (board[r][c].value == UNSOLVED)
{
unsolved.push_back(&board[r][c]);
}
}
return unsolved;
}
int getVal(char c)
{
if (c == UNSOLVED)
{
return 0;
}
return c - '0';
}
void checkValue(char c, int &values)
{
int v = getVal(c);
if (!v)
{
return;
}
if (values & (1 << v))
{
throw runtime_error("invalid");
}
values |= (1 << v);
}
bool checkRow(int row, int column, Cells &board, int &values)
{
checkValue(board[row][column].value, values);
return true;
}
bool checkColumn(int row, int column, Cells &board, int &values)
{
checkValue(board[row][column].value, values);
return true;
}
char getCell(int box, Cells &board, int i)
{
int firstRow = (box / _R) * _R;
int firstCol = (box % _C) * _C;
int row = firstRow + i / _R;
int col = firstCol + (i % _C);
return board[row][col].value;
}
bool checkBox(int box, int cell, Cells &board, int &values)
{
char c = getCell(box, board, cell);
checkValue(c, values);
return true;
}
private:
vector<Cell *> _unsolved;
Cells _cells;
int _R;
int _C;
int _M;
int _N;
bool _solved;
};
// class definition comes from leetcode
class Solution
{
public:
void solveSudoku(Chars &board)
{
_sudoku.from(board);
_sudoku.solve();
_sudoku.to(board);
}
void display(Cells &board, bool code = false)
{
_sudoku.display(board, code);
}
private:
Sudoku _sudoku;
};
TEST(SudokuSolver, Solved)
{
Sudoku sudoku;
Chars board =
{{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
sudoku.from(board);
ASSERT_EQ(sudoku.check(), true);
}
TEST(SudokuSolver, NotSolved)
{
Sudoku sudoku;
Chars board =
{{'.', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
sudoku.from(board);
ASSERT_EQ(sudoku.check(), false);
}
TEST(SudokuSolver, Simple)
{
Solution solution;
Chars board =
{{'.', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
Chars solved =
{{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
solution.solveSudoku(board);
ASSERT_TRUE(board == solved);
}
TEST(SudokuSolver, TooSimple)
{
Solution solution;
Chars board =
{{'.', '.', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
Chars solved =
{{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
solution.solveSudoku(board);
ASSERT_TRUE(board == solved);
}
TEST(SudokuSolver, Example)
{
Solution solution;
Chars board =
{{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
{'.', '.', '.', '.', '8', '.', '.', '7', '9'}};
Chars solved =
{{'5', '3', '4', '6', '7', '8', '9', '1', '2'},
{'6', '7', '2', '1', '9', '5', '3', '4', '8'},
{'1', '9', '8', '3', '4', '2', '5', '6', '7'},
{'8', '5', '9', '7', '6', '1', '4', '2', '3'},
{'4', '2', '6', '8', '5', '3', '7', '9', '1'},
{'7', '1', '3', '9', '2', '4', '8', '5', '6'},
{'9', '6', '1', '5', '3', '7', '2', '8', '4'},
{'2', '8', '7', '4', '1', '9', '6', '3', '5'},
{'3', '4', '5', '2', '8', '6', '1', '7', '9'}};
solution.solveSudoku(board);
ASSERT_TRUE(board == solved);
}
TEST(SudokuSolver, Test1)
{
Solution solution;
Chars board =
{{'.', '.', '.', '.', '.', '7', '.', '.', '9'},
{'.', '4', '.', '.', '8', '1', '2', '.', '.'},
{'.', '.', '.', '9', '.', '.', '.', '1', '.'},
{'.', '.', '5', '3', '.', '.', '.', '7', '2'},
{'2', '9', '3', '.', '.', '.', '.', '5', '.'},
{'.', '.', '.', '.', '.', '5', '3', '.', '.'},
{'8', '.', '.', '.', '2', '3', '.', '.', '.'},
{'7', '.', '.', '.', '5', '.', '.', '4', '.'},
{'5', '3', '1', '.', '7', '.', '.', '.', '.'}};
Chars solved =
{{'3', '1', '2', '5', '4', '7', '8', '6', '9'},
{'9', '4', '7', '6', '8', '1', '2', '3', '5'},
{'6', '5', '8', '9', '3', '2', '7', '1', '4'},
{'1', '8', '5', '3', '6', '4', '9', '7', '2'},
{'2', '9', '3', '7', '1', '8', '4', '5', '6'},
{'4', '7', '6', '2', '9', '5', '3', '8', '1'},
{'8', '6', '4', '1', '2', '3', '5', '9', '7'},
{'7', '2', '9', '8', '5', '6', '1', '4', '3'},
{'5', '3', '1', '4', '7', '9', '6', '2', '8'}};
solution.solveSudoku(board);
ASSERT_TRUE(board == solved);
}