Skip to content

Commit 6f2a09a

Browse files
authored
Merge pull request #21 from shimmer12/main
Added 37. Sudoku Solver.cpp
2 parents dc1af96 + fad00e7 commit 6f2a09a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

37. Sudoku Solver.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
void Sudokosolr(vector<vector<char>>& board) {
4+
sol(board, 0);
5+
}
6+
7+
private:
8+
bool sol(vector<vector<char>>& board, int s) {
9+
if (s == 81)
10+
return true;
11+
12+
const int i = s / 9;
13+
const int j = s % 9;
14+
15+
if (board[i][j] != '.')
16+
return sol(board, s + 1);
17+
18+
for (char a= '1'; a<= '9'; ++a)
19+
if (isValid(board, i, j, a)) {
20+
board[i][j] = a;
21+
if (sol(board, s + 1))
22+
return true;
23+
board[i][j] = '.';
24+
}
25+
26+
return false;
27+
}
28+
29+
bool isValid(vector<vector<char>>& board, int row, int col, char a) {
30+
for (int i = 0; i < 9; ++i)
31+
if (board[i][col] == a|| board[row][i] == a||
32+
board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == a)
33+
return false;
34+
return true;
35+
}
36+
};

0 commit comments

Comments
 (0)