forked from Nimesh-Srivastava/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0037.cpp
More file actions
58 lines (44 loc) · 1.52 KB
/
0037.cpp
File metadata and controls
58 lines (44 loc) · 1.52 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
class Solution {
bool checkCell(vector<vector<char>>& board, int row, int col, int i, char c){
char cal = board[3 * (row / 3) + (i / 3)][3 * (col / 3) + (i % 3)];
if(cal == c)
return false;
return true;
}
bool valid(char c, vector<vector<char>>& board, int row, int col){
for(int i = 0; i < 9; i++){
if(board[i][col] == c)
return false;
if(board[row][i] == c)
return false;
if(!checkCell(board, row, col, i, c))
return false;
}
return true;
}
bool solve(vector<vector<char>>& board){
int m = board.size();
int n = board[0].size();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == '.'){
for(char c = '1'; c <= '9'; c++){
if(valid(c, board, i, j)){
board[i][j] = c;
if(solve(board))
return true;
else
board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
public:
void solveSudoku(vector<vector<char>>& board) {
solve(board);
}
};