-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku_solver_striver.c++
53 lines (51 loc) · 1.64 KB
/
Sudoku_solver_striver.c++
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
class Solution {
public:
bool possible(char num, vector<vector<char>>& board, int row, int col) {
// Check the current row
for (int i = 0; i < board.size(); i++) {
if (board[row][i] == num) {
return false;
}
}
// Check the current column
for (int i = 0; i < board[0].size(); i++) {
if (board[i][col] == num) {
return false;
}
}
// Check the 3x3 subgrid
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 (board[i][j] == num) {
return false;
}
}
}
return true;
}
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 (possible(c, board, i, j)) {
board[i][j] = c;
if (solve(board)) {
return true;
} else {
board[i][j] = '.';
}
}
}
return false;
}
}
}
return true;
}
void solveSudoku(vector<vector<char>>& board) {
solve(board);
}
};