-
Notifications
You must be signed in to change notification settings - Fork 2
/
sudoku_solver.cpp
175 lines (160 loc) · 3.6 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
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
170
171
172
173
174
175
#include <bits/stdc++.h>
using namespace std;
#define vi vector <int>
#define pii pair < int,int >
#define pvii pair < vi , pii >
#define pb push_back
#define mp make_pair
#define fr first
#define se second
#define vvi vector < vi >
map<vvi,int> already;
int flag=0;
void print(vvi ans)
{
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
cout<<ans[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
bool check( vvi cur )
{
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
if (cur[i][j] == 0)
return false;
}
}
return true;
}
inline vi fillone(vi canbe)
{
for (int i=0;i<10;i++)
canbe[i]=1;
return canbe;
}
inline vi rowcheck( vvi cur , vi canbe , int r , int c )
{
for (int i=0;i<9;i++){
if (cur[r][i] != 0)
canbe[cur[r][i]]=0;
}
return canbe;
}
inline vi colcheck( vvi cur , vi canbe , int r , int c )
{
for (int i=0;i<9;i++){
if (cur[i][c] != 0)
canbe[cur[i][c]]=0;
}
return canbe;
}
inline vi blockcheck( vvi cur, vi canbe , int r, int c )
{
int rr=r-r%3;
int cc=c-c%3;
for (int i=rr;i<rr+3;i++){
for(int j=cc;j<cc+3;j++){
if (cur[i][j] != 0)
canbe[cur[i][j]]=0;
}
}
return canbe;
}
int degree_heuristic( vvi cur , int r , int c )
{
int counter = 0;
for (int i=0;i<9;i++){
counter += (cur[r][i] == 0) ? 1 : 0;
counter += (cur[i][c] == 0) ? 1 : 0;
}
int rr=r-r%3;
int cc=c-c%3;
for (int i=rr;i<rr+3;i++){
for(int j=cc;j<cc+3;j++){
if (i== r || j==c) continue;
counter += (cur[i][j] == 0) ? 1 : 0;
}
}
return counter;
}
pvii mrv_heuristic ( vvi cur )
{
int mrvmin = INT_MAX;
int degmin = INT_MIN;
pvii val;
for (int i=0;i<9;i++){
for (int j=0;j<9;j++){
if (cur[i][j] == 0){
vi canbe(10);
canbe = fillone(canbe);
canbe = rowcheck(cur,canbe,i,j);
canbe = colcheck(cur,canbe,i,j);
canbe = blockcheck(cur,canbe,i,j);
int counter = 0;
for (int k=1;k<=9;k++){
if (canbe[k] == 1)
counter++;
}
int degval = degree_heuristic(cur,i,j);
if (counter < mrvmin){
mrvmin = counter;
degmin = degval;
val = mp(canbe,mp(i,j));
}
else if (counter == mrvmin && degval > degmin){
degmin = degval;
val = mp(canbe,mp(i,j));
}
}
}
}
return val;
}
int solve( vvi cur )
{
if (check(cur)){
flag=1;
print(cur);
return 1;
}
if(already.find(cur)!=already.end())
return 0;
already[cur] = 1;
pvii mrvval = mrv_heuristic(cur);
vi canbe = mrvval.fr;
int i = mrvval.se.fr;
int j = mrvval.se.se;
for (int k=1;k<=9;k++){
if (canbe[k] == 1){
cur[i][j] = k;
int p = solve(cur);
if(p==1)
return 1;
cur[i][j] = 0;
}
}
return 0;
}
int main ()
{
freopen("sudoku_input.txt","r",stdin);
freopen("sudoku_output.txt","w",stdout);
ios_base::sync_with_stdio(false);
vvi start;
for (int i=0;i<9;i++){
vi temp;
for (int j=0;j<9;j++){
int foo;
cin>>foo;
temp.pb(foo);
}
start.pb(temp);
}
int res = solve(start);
if(res==0)
cout<<"-1";
}