-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path0037-sudoku-solver.cpp
More file actions
42 lines (38 loc) · 1.34 KB
/
Copy path0037-sudoku-solver.cpp
File metadata and controls
42 lines (38 loc) · 1.34 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
class Solution {
public:
bool col[9][9], row[9][9], box[9][9];
bool fill(vector<vector<char>>& board, int idx) {
if (idx == 81) return true;
int x = idx / 9, y = idx % 9;
char p = board[x][y];
if (p != '.') {
return fill(board, idx + 1);
} else {
for (int i = 0; i < 9; ++i) {
if (row[x][i] || col[y][i] || box[x / 3 * 3 + y / 3][i]) continue;
row[x][i] = col[y][i] = box[x / 3 * 3 + y / 3][i] = true;
board[x][y] = '1' + i;
if (fill(board, idx + 1)) return true;
board[x][y] = '.';
row[x][i] = col[y][i] = box[x / 3 * 3 + y / 3][i] = false;
}
return false;
}
}
void solveSudoku(vector<vector<char>>& board) {
memset(col, 0, sizeof(col));
memset(row, 0, sizeof(row));
memset(box, 0, sizeof(box));
for (int i = 0; i < board.size(); ++i) {
for (int j = 0; j < board[i].size(); ++j) {
char p = board[i][j];
if (p != '.') {
row[i][p - '1'] = true;
col[j][p - '1'] = true;
box[i / 3 * 3 + j / 3][p - '1'] = true;
}
}
}
fill(board, 0);
}
};