-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path37.cpp
More file actions
61 lines (61 loc) · 1.82 KB
/
37.cpp
File metadata and controls
61 lines (61 loc) · 1.82 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
class Solution {
public:
bool success = false;
bool isValid(vector<vector<char>>& board, int x, int y) {
// row
int mask = 0;
for (int j = 0; j < 9; ++j) {
if (board[x][j] == '.') continue;
int num = 1 << (board[x][j] - '0');
if (mask & num) return false;
mask |= num;
}
// col
mask = 0;
for (int i = 0; i < 9; ++i) {
if (board[i][y] == '.') continue;
int num = 1 << (board[i][y] - '0');
if (mask & num) return false;
mask |= num;
}
// grid
mask = 0;
for (int i = x / 3 * 3; i < x / 3 * 3 + 3; ++i) {
for (int j = y / 3 * 3; j < y / 3 * 3 + 3; ++j) {
//cout << "i: " << i << " j: " << j << endl;
if (board[i][j] == '.') continue;
int num = 1 << (board[i][j] - '0');
if (mask & num) return false;
mask |= num;
}
}
return true;
}
void _solve(vector<vector<char>>& board, int index, vector<vector<char>>& res) {
if (index == 81) {
res = board;
return;
}
int x = index / 9;
int y = index % 9;
if (board[x][y] != '.') {
_solve(board, index + 1, res);
}
else {
for (int k = 1; k <= 9; ++k) {
board[x][y] = k + '0';
if (!isValid(board, x, y)) {
board[x][y] = '.';
continue;
}
_solve(board, index + 1, res);
board[x][y] = '.';
}
}
}
void solveSudoku(vector<vector<char>>& board) {
vector<vector<char>> res(9, vector<char>(9, '.'));
_solve(board, 0, res);
board = res;
}
};