-
Notifications
You must be signed in to change notification settings - Fork 776
/
Sudoku_Solver.cpp
58 lines (50 loc) · 1.47 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
#include <iostream>
#include <vector>
using namespace std;
const int N = 9;
bool isValid(vector<vector<char>>& board, int row, int col, char num) {
for (int i = 0; i < N; i++) {
if (board[row][i] == num || board[i][col] == num || board[3 * (row / 3) + i / 3][3 * (col / 3) + i % 3] == num)
return false;
}
return true;
}
bool solveSudoku(vector<vector<char>>& board) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
if (board[row][col] == '.') {
for (char num = '1'; num <= '9'; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board))
return true;
board[row][col] = '.'; // Backtrack
}
}
return false;
}
}
}
return true;
}
int main() {
vector<vector<char>> board(N, vector<char>(N));
// Input the Sudoku board here
// '.' represents empty cells
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> board[i][j];
}
}
if (solveSudoku(board)) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << board[i][j] << " ";
}
cout << endl;
}
} else {
cout << "No solution exists." << endl;
}
return 0;
}