-
Notifications
You must be signed in to change notification settings - Fork 0
/
036valid_sudoku.cpp
50 lines (48 loc) · 1.3 KB
/
036valid_sudoku.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
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
//set a array to store placehold;
bool used[9];
for(int i=0; i<9; ++i){
fill(used, used+9, false); //initialize as false;
/*check one row*/
for(int j=0; j<9; ++j){
if(!check(board[i][j], used)){
return false;
}
}
/*check one column*/
fill(used, used+9, false);
for(int j=0; j<9; ++j){
if(!check(board[j][i], used)){
return false;
}
}
}
for(int r=0; r<3; ++r){
for(int c=0; c<3; ++c){
fill(used, used+9, false);
for(int i=r*3; i<r*3+3; ++i){
for(int j=c*3; j<c*3+3; ++j){
if(!check(board[i][j], used)){
return false;
}
}
}
}
}
return true;
}
bool check(char ch, bool used[9]){
if(ch == '.'){
return true;
}
else if(used[ch-'1']){
return false;
}
else{
used[ch-'1'] = true;
return true;
}
}
};