forked from kishanrajput23/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.cpp
169 lines (161 loc) · 3.04 KB
/
Sudoku.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include<iostream>
using namespace std;
void printSolution(int **grid)
{
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
cout<<grid[i][j]<<" ";
}
cout<<endl;
}
}
bool UsedInRow(int **grid,int row ,int num)
{
for(int i=0;i<9;i++)
{
if(grid[row][i]==num)
{
return true;
}
}
return false;
}
bool UsedInColumn(int **grid,int col,int num)
{
for(int i=0;i<9;i++)
{
if(grid[i][col]==num)
{
return true;
}
}
return false;
}
bool UsedInBox(int **grid,int row,int col,int num)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(grid[row+i][col+j]==num)
{
return true;
}
}
}
return false;
}
bool isSafe(int **grid,int row,int col,int num)
{
if(UsedInRow(grid,row,num)==false
&& UsedInBox(grid,row-row%3,col-col%3,num)==false
&& UsedInColumn(grid,col,num)==false && grid[row][col]==0)
{
return true;
}
else
{
return false;
}
}
bool findUnassignedPosition(int **grid,int &row,int &col)
{
for(row=0;row<9;row++)
{
for(col=0;col<9;col++)
{
if(grid[row][col]==0)
{
return true;
}
}
}
return false;
}
bool solveSudokuUtil(int **grid,int row,int col)
{
if(findUnassignedPosition(grid,row,col)==false)
{
return true;
}
else
{
for(int i=1;i<=9;i++)
{
if(isSafe(grid,row,col,i)==true)
{
grid[row][col]=i;
if(solveSudokuUtil(grid,row,col)==true)
{
return true;
}
else
{
grid[row][col]=0;
}
}
}
return false;
}
}
void solveSudoku(int **grid)
{
if(solveSudokuUtil(grid,0,0)==true)
{
printSolution(grid);
}
else
{
cout<<"Solution does not exists";
}
}
int main()
{
int **grid=new int*[9];
for(int i=0;i<9;i++)
{
grid[i]=new int[9];
}
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
grid[i][j]=0;
}
}
grid[0][0]=3;
grid[0][2]=6;
grid[0][3]=5;
grid[0][5]=8;
grid[0][6]=4;
grid[1][0]=5;
grid[1][1]=2;
grid[2][1]=8;
grid[2][2]=7;
grid[2][7]=3;
grid[2][8]=1;
grid[3][2]=3;
grid[3][4]=1;
grid[3][7]=8;
grid[4][0]=9;
grid[4][3]=8;
grid[4][4]=6;
grid[4][5]=3;
grid[4][8]=5;
grid[5][1]=5;
grid[5][4]=9;
grid[5][6]=6;
grid[6][0]=1;
grid[6][1]=3;
grid[6][6]=2;
grid[6][7]=5;
grid[7][7]=7;
grid[7][8]=4;
grid[8][2]=5;
grid[8][3]=2;
grid[8][5]=6;
grid[8][6]=3;
solveSudoku(grid);
}