-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path7 August Solve the Sudoku
51 lines (50 loc) · 1.45 KB
/
7 August Solve the Sudoku
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
class Solution
{
public:
//Function to find a solved Sudoku.
int mp[9][2]={{0,2},{0,2},{0,2},{3,5},{3,5},{3,5},{6,8},{6,8},{6,8}};
bool isSafe(int grid[N][N],int r,int c,int num){
for(int i=0;i<9;i++){
if(grid[i][c]==num || grid[r][i]==num)
return false;
}
for(int i=mp[r][0];i<=mp[r][1];i++){
for(int j=mp[c][0];j<=mp[c][1];j++){
if(grid[i][j]==num)
return false;
}
}
return true;
}
bool SolveSudoku(int grid[N][N])
{
// Your code here
for(int r=0;r<N;r++){
for(int c=0;c<N;c++){
if(grid[r][c]==0){
for(int num=1;num<=9;num++){
if(isSafe(grid,r,c,num)){
grid[r][c]=num;
if(SolveSudoku(grid))
return true;
grid[r][c]=0;
}
}
return false;
}
}
}
return true;
}
//Function to print grids of the Sudoku.
void printGrid (int grid[N][N])
{
// Your code here
if(SolveSudoku(grid)){
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
cout<<grid[i][j]<<" ";
}
else cout<<"No solution exists"<<endl;
}
};