-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathSudoku solver.cpp
59 lines (50 loc) · 1.74 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void solveSudoku(vector<vector<char>>& board) {
stack<pair<int, int>> emptyCells;
// Find and store the indices of empty cells in the stack
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') {
emptyCells.push({i, j});
}
}
}
int index = 0;
while (!emptyCells.empty()) {
int row = emptyCells.top().first;
int col = emptyCells.top().second;
emptyCells.pop();
bool found = false;
for (char c = '1'; c <= '9'; c++) {
if (issafe(board, row, col, c)) {
board[row][col] = c;
found = true;
break;
}
}
if (!found) {
board[row][col] = '.'; // Reset cell value
index--;
while (index < 0) {
index = emptyCells.size() - 1;
if (index < 0) break;
row = emptyCells.top().first;
col = emptyCells.top().second;
emptyCells.pop();
board[row][col] = '.';
}
} else {
index = emptyCells.size() - 1;
}
}
}
bool issafe(vector<vector<char>>& board, int row, int col, char val) {
// Check row, column, and 3x3 subgrid
int toprow = row - row % 3;
int topcol = col - col % 3;
for (int i = 0; i < 9; i++) {
if (board[row][i] == val || board[i][col] == val || board[toprow + i / 3][topcol + i % 3] == val) {
return false;
}
}
return true;
}