-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudokuSolver.cpp
54 lines (48 loc) · 1.79 KB
/
SudokuSolver.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
#include<bits/stdc++.h>
#include "../utilities.h"
using namespace std;
class Solution {
bool isValid(int row, int col, char c, vector<vector<char>>& board) {
for (int i = 0; i < 9; i++) {
if (board[row][i] == c) return false;
if (board[i][col] == c) return false;
if (board[3 * (row/3) + i / 3][3 * (col/3) + i % 3] == c) return false;
}
return true;
}
bool findPossibleBoard(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 (isValid(i, j, c, board)) {
board[i][j] = c;
if (findPossibleBoard(board) == true) return true;
else board[i][j] = '.';
}
}
return false;
}
}
}
return true;
}
public:
void solveSudoku(vector<vector<char>>& board) {
findPossibleBoard(board);
}
};
int main() {
Solution s;
vector<vector<char>> board{{'5','3','.','.','7','.','.','.','.'},
{'6','.','.','1','9','5','.','.','.'},
{'.','9','8','.','.','.','.','6','.'},
{'8','.','.','.','6','.','.','.','3'},
{'4','.','.','8','.','3','.','.','1'},
{'7','.','.','.','2','.','.','.','6'},
{'.','6','.','.','.','.','2','8','.'},
{'.','.','.','4','1','9','.','.','5'},
{'.','.','.','.','8','.','.','7','9'}};
s.solveSudoku(board);
cout<< board;
}