-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsudokuSolver.cpp
118 lines (101 loc) · 3.25 KB
/
sudokuSolver.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
#include <vector>
#include <iostream>
#define BLANK 0
using namespace std;
class Solver {
public:
// vector<string> digits = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
// vector<string> rows = {'A', 'B', 'C',' D', 'E', 'F', 'G', 'H', 'I'};
// vector<string> cols = digits;
// vector<string> squares = cross(rows, cols);
vector<int> serialGridToSolve;
vector<vector<int>> workingGrid;
vector<int> solveSudoku(vector<int> serialGrid) {
if (serialGrid != serialGridToSolve) {
// printf("Found new puzzle\n");
serialGridToSolve = serialGrid;
workingGrid = deserialize(serialGridToSolve);
solve(workingGrid);
}
return serialize(workingGrid);
}
bool solve(vector<vector<int>> grid) {
int row;
int col;
if (!findBlank(grid, row, col)) {
workingGrid = grid;
return true;
}
for (int num = 1; num <= 9; num++) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;
if (solve(grid)) {
return true;
} else {
grid[row][col] = BLANK;
}
}
}
return false;
}
private:
vector<vector<int>> deserialize(vector<int> serialGrid) {
vector<vector<int>> grid;
for (int i = 0; i < 9; i++) {
vector<int> subsection(serialGrid.begin() + i*9, serialGrid.begin() + (i+1)*9);
grid.push_back(subsection);
}
return grid;
}
vector<int> serialize(vector<vector<int>> grid) {
vector<int> serialGrid;
for (vector<int> row : grid) {
for (int num : row) {
serialGrid.push_back(num);
}
}
return serialGrid;
}
// vector<string> cross(vector<string> a, vector<string> b) {
// vector<string> product;
// for (string x: a) {
// for (string y: b) {
// product.push_back(x+y);
// }
// }
// return product;
// }
bool findBlank(vector<vector<int>> grid, int &row, int &col) {
for (row = 0; row < 9; row++) {
for (col = 0; col < 9; col++) {
if (grid[row][col] == BLANK) return true;
}
}
return false;
}
bool usedInRow(vector<vector<int>> grid, int row, int num) {
for (int col = 0; col < 9; col++) {
if (grid[row][col] == num) return true;
}
return false;
}
bool usedInCol(vector<vector<int>> grid, int col, int num) {
for (int row = 0; row < 9; row++) {
if (grid[row][col] == num) return true;
}
return false;
}
bool usedInBox(vector<vector<int>> grid, int boxStartRow, int boxStartCol, int num) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
if (grid[row+boxStartRow][col+boxStartCol] == num) return true;
}
}
return false;
}
bool isSafe(vector<vector<int>> grid, int row, int col, int num) {
return !usedInRow(grid, row, num) &&
!usedInCol(grid, col, num) &&
!usedInBox(grid, row - row%3, col - col%3, num);
}
};