Skip to content

Commit 4d2f4c8

Browse files
Merge pull request #503 from tandrimasingha/main
Sudoku-Solver
2 parents 7c053ac + 3466d89 commit 4d2f4c8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Sudoku-Solver.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
void solveSudoku(vector<vector<char>>& board) {
4+
solve(board);
5+
}
6+
7+
bool solve(vector<vector<char>>& board){
8+
for(int i=0; i<board.size(); i++){
9+
for(int j=0; j<board[0].size(); j++){
10+
if(board[i][j] == '.'){
11+
for(char c='1'; c<='9'; c++){
12+
if(valid(c, board, i, j)){
13+
board[i][j] = c;
14+
if(solve(board) == true) return true;
15+
else board[i][j]='.';
16+
}
17+
}
18+
return false;
19+
}
20+
}
21+
}
22+
return true;
23+
}
24+
25+
bool valid(char c, vector<vector<char>>& board, int row, int col){
26+
for(int i=0; i<9; i++){
27+
if(board[i][col] == c) return false;
28+
if(board[row][i] == c) return false;
29+
if(board[3*(row/3)+(i/3)][3*(col/3)+(i%3)] == c) return false;
30+
}
31+
return true;
32+
}
33+
};

0 commit comments

Comments
 (0)