-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#129.cc
More file actions
53 lines (48 loc) · 1.88 KB
/
Copy pathLeetCode#129.cc
File metadata and controls
53 lines (48 loc) · 1.88 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
class Solution {
private:
bool dfs(vector<vector<char> >& board,
vector<int>& row,vector<int>& col,
vector<int>& blk, int ind){
if(ind==81) return true;
int i = ind/9;
int j = ind%9;
if(board[i][j]=='.'){
for(int k=1;k<=9;k++){
if((row[i]&(1<<k))==0 &&
(col[j]&(1<<k))==0 &&
(blk[i/3*3+j/3]&(1<<k))==0){
row[i]|=(1<<k);
col[j]|=(1<<k);
blk[i/3*3+j/3]|=(1<<k);
board[i][j]=(char)('0'+k);
bool ret = dfs(board,row,col,blk,ind+1);
if(ret) return true;
board[i][j]='.';
row[i]&=~(1<<k);
col[j]&=~(1<<k);
blk[i/3*3+j/3]&=~(1<<k);
}
}
}
else return dfs(board,row,col,blk,ind+1);
return false;
}
public:
void solveSudoku(vector<vector<char> > &board) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> row(9,0);
vector<int> col(9,0);
vector<int> blk(9,0);
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
if(board[i][j]!='.'){
int k = board[i][j]-'0';
row[i]|=(1<<k);
col[j]|=(1<<k);
blk[i/3*3+j/3]|=(1<<k);
//board[i][j]=(char)('0'+k);
}
dfs(board,row,col,blk,0);
}
};