-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path36. Valid Sudoku.cpp
57 lines (57 loc) · 1.52 KB
/
36. Valid 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
51
52
53
54
55
56
57
class Solution {
private:
bool solve(vector<vector<char>>& board, vector<vector<bool>>& visited){
for(int i=0;i<board.size();i++){
for(int j=0;j<board[0].size();j++){
if(board[i][j] != '.' and visited[i][j] == false){
visited[i][j] = true;
if(canPlace(board, i, j, board[i][j])){
if(solve(board, visited))
return true;
else
return false;
}else{
return false;
}
}
}
}
return true; //Base case we have reached the solution we return
}
bool canPlace(vector<vector<char>>& board, int row, int col, char c){
//We have ensure that we are not checking the same character with itself.
for(int i=0;i<9;i++){
if(i != col){
if(board[row][i] == c)
return false;
}
if(i != row){
if(board[i][col] == c)
return false;
}
int x = 3 * (row/3) + i/3;
int y = 3 * (col/3) + i%3;
if((x) != row and (y) != col){
if(board[x][y] == c)
return false;
}
}
return true;
}
public:
bool isValidSudoku(vector<vector<char>>& board) {
int n = board.size();
int m = board[0].size();
vector<vector<bool>> visited(n, vector<bool>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(board[i][j] != '.'){
visited[i][j] = false;
}else{
visited[i][j] = true;
}
}
}
return solve(board, visited);
}
};