forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku-Solver.cpp
33 lines (31 loc) · 1022 Bytes
/
Sudoku-Solver.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
class Solution {
public:
void solveSudoku(vector<vector<char>>& board) {
solve(board);
}
bool solve(vector<vector<char>>& board){
for(int i=0; i<board.size(); i++){
for(int j=0; j<board[0].size(); 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) == true) return true;
else board[i][j]='.';
}
}
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(board[3*(row/3)+(i/3)][3*(col/3)+(i%3)] == c) return false;
}
return true;
}
};