-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path37.sudoku-solver.cpp
More file actions
57 lines (47 loc) · 1.48 KB
/
37.sudoku-solver.cpp
File metadata and controls
57 lines (47 loc) · 1.48 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
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
solve(board, 0, 0);
}
bool solve(vector<vector<char>>& board, int row, int col){
int n=board.size();
if(row >= n)
return true;
if(col>=n)
return solve(board, row+1, 0);
if(board[row][col] != '.')
return solve(board, row, col+1);
for(char c = '1'; c<='9'; c++){
board[row][col] = c;
if(check(board, row, col)){
if(solve(board, row, col + 1))
return true;
}
}
board[row][col] = '.';
return false;
}
bool check(vector<vector<char>>& board, int row, int col){
for(int j = 0; j<board[0].size(); j++){
if(j != col && board[row][j] == board[row][col]){
return false;
}
}
for(int i=0; i<board.size(); i++){
if(i != row && board[i][col] == board[row][col]){
return false;
}
}
int startRow = (row/3)*3;
int startCol = (col/3)*3;
for(int i=startRow; i<startRow+3; i++){
for(int j=startCol; j<startCol+3; j++){
if(i == row && j == col)
continue;
if(board[i][j] == board[row][col])
return false;
}
}
return true;
}
};