-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSudokuSolver.cpp
More file actions
49 lines (49 loc) · 1.56 KB
/
Copy pathSudokuSolver.cpp
File metadata and controls
49 lines (49 loc) · 1.56 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
class Solution {public:
bool isSafe(int row, int col, vector<vector<char>>& sudoku, char val){
int n = sudoku.size();
// Row and Column Check
for(int i = 0; i < n; i++){
if(sudoku[row][i] == val || sudoku[i][col] == val){
return false;
}
}
// 3x3 matrix Check
// int boxStartRow = row - row % 3;
// int boxStartCol = col - col % 3;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(sudoku[i + row - row % 3][j + col - col % 3] == val){
return false;
}
}
}
return true;
}
bool solve(vector<vector<char>>& sudoku){
int n = sudoku.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
//Finding if cell is empty or not
if(sudoku[i][j] == '.'){
for(char val = '1'; val <= '9'; val++){
if(isSafe(i, j, sudoku, val)){
sudoku[i][j] = val;
//recursive call
if(solve(sudoku)){
return true;
}
else{
//backtrack
sudoku[i][j] = '.';
}
}
}
return false;
}
}
}
return true;
}
void solveSudoku(vector<vector<char>>& board) {
solve(board);
}};