-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0037____Sudoku_Solver.cpp
69 lines (65 loc) · 2.69 KB
/
0037____Sudoku_Solver.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
#include "util\frequently_included.h"
inline int get_sub_index(int row, int col){
return (row / 3) * 3 + col / 3;
}
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
_1(board);
}
private:
void _1(vector<vector<char>>& board){
vector<int> sub(9, 0);
for(int i = 0; i < 9; ++i)
for(int ii = 0; ii < 9; ++ii)
if(board[i][ii] != '.'){
char temp = board[i][ii] - '0';
sub[get_sub_index(i, ii)] |= 1 << temp;
}
_1_dfs(board, sub, 0, 0);
}
bool _1_dfs(vector<vector<char>>& board, vector<int>& sub, int i, int ii){
if(ii == 9){
i++;
ii = 0;
}
if(i == 9)
return true;
if(board[i][ii] == '.'){
for(int mask = 1; mask <= 9; ++mask){
int temp = 1 << mask;
if(!(temp & row[i]) && !(temp & col[i]) && !(temp & sub[get_sub_index(i, ii)])){
row[i] |= temp;
col[ii] |= temp;
sub[get_sub_index(i, ii)] |= temp;
board[i][ii] = '0' + mask;
if(_1_dfs(board, row, col, sub, i, ii + 1))
return true;
row[i] ^= temp;
col[ii] ^= temp;
sub[get_sub_index(i, ii)] ^= temp;
board[i][ii] = '.';
}
}
return false;
}else{
return _1_dfs(board, row, col, sub, i, ii + 1);
}
}
};
int main(){
vector<vector<char>> 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'}
};
Solution sln;
sln.solveSudoku(board);
return 0;
}