-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Sudoku_Solver.cpp
58 lines (47 loc) · 1.13 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;
bool isSafe(int row, int col,vector<vector<int>> &board, int val)
{
for(int i=0; i<board.size(); i++)
{
if(board[row][i]==val)
return false;
if(board[i][col]==val)
return false;
if(board[3*(row/3)+i/3][3*(col/3) + i%3]==val)
return false;
}
return true;
}
bool solve(vector<vector<int>> &board)
{
int n = board.size();
for(int i=0; i<n; i++)
{
for(int j=0; i<n; j++)
{
if(board[i][j]==0) // block is empty
{
for(int val=1; val<=9; val++)
{
if(isSafe(i,j,board,val))
{
board[i][j] = val;
bool possible = solve(board);
if(possible)
return true;
else
board[i][j] = 0;
}
}
return false;
}
}
}
return true;
}
int main()
{
return 0;
}