forked from lafolle/Agastya
/
SUDOKU.cpp
219 lines (184 loc) · 3.59 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <map>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <queue>
#include <cassert>
#include <cstring>
using namespace std;
#define pl cout<<endl;
#define all(v) v.begin(),v.end()
#define sz(v) v.size()
#define SENTINEL 1000001
#define ii(x,y) make_pair(x,y)
typedef long long int lli;
typedef vector<string, string> vpss;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef map<string, int> msi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef set<int> SI;
typedef set<int>::iterator SI_it;
int BRD[16][16];
int depth_sol;
struct State{
int M[16][16],i,j,k,p,q;
int depth;
int nv_x,nv_y; // next vacant pos
SI s,rst,cst;
State(){}
State(int mat[16][16],int d, int nvx,int nvy){
for(i=0;i<16;i++)for(j=0;j<16;j++)M[i][j]=mat[i][j];
depth=d;
nv_x=nvx;
nv_y=nvy;
}
bool isgoal(){
for(p=0;p<16;p+=4)
for(q=0;q<16;q+=4){
for(i=p;i<p+4;i++)
for(j=q;j<q+4;j++)
if(M[i][j]!=0&&(s.insert(M[i][j])).second==false)
return false;
s.clear();
}
for(i=0;i<16;i++){
s.clear();
for(j=0;j<16;j++)
if(M[i][j]==0||(s.insert(M[i][j])).second==false)
return false;
}
for(i=0;i<16;i++){
s.clear();
for(j=0;j<16;j++)
if(M[j][i]==0||(s.insert(M[j][i])).second==false)
return false;
}
return true;
}
void show()
{
for(i=0;i<16;i++){
for(j=0;j<16;j++)
printf("%d ", M[i][j]);
printf("\n");
}pl;
fflush(stdout);
}
void spojshow(){
for(i=0;i<16;i++){
for(j=0;j<16;j++)
printf("%c",'A'+M[i][j]+1);
pl;
}
}
};
bool possible(int k, int x, int y, int M[16][16])
{
int bx=(x/4)*4,by=(y/4)*4,i,j;
for(i=0;i<16;i++)if(M[x][i]==k)return 0;
for(i=0;i<16;i++)if(M[i][y]==k)return 0;
for(i=bx;i<bx+4;i++)
for(j=by;j<by+4;j++)
if(i!=x&&j!=y&&M[i][j]==k) return 0;
return 1;
}
pii getnextvacant(int x,int y)
{
int i,j;
if(y==15&&x==15) return ii(-1,-1);
if(y==15){
i=x+1;
j=0;
}
else{
i=x;
j=y+1;
}
for(;i<16;i++,j=0)
for(;j<16;j++)
if(!BRD[i][j])return ii(i,j);
return ii(-1,-1);
}
void solve(State root)
{
int i,j,k=0,mat[16][16],x,y;
stack<State> S;
State ts;
pii vp;
S.push(root);
while(!S.empty()){
ts=S.top();S.pop();
if(ts.depth==depth_sol && ts.isgoal()){
ts.spojshow();
return;
}
x=ts.nv_x;y=ts.nv_y;
if(x==-1&&y==-1)continue;
if(k==100000){
ts.show();
k=0;
}
++k;
for(i=0;i<16;i++)for(j=0;j<16;j++)mat[i][j]=ts.M[i][j];
vp=getnextvacant(x,y);
for(i=1;i<=16;i++){
if(!possible(i,x,y,mat))continue;
mat[x][y]=i;
S.push(State(mat,ts.depth+1,vp.first,vp.second));
}
}
}
int main()
{
int M[16][16],i,j,k,t,x,y;
char line[16];
scanf("%d",&t);
while(t--){
depth_sol=0;
for(i=0;i<16;i++){
scanf("%s",line);
printf("%s\n",line);
for(j=0;j<16;j++){
if(line[j]=='-'){
M[i][j]=0;
BRD[i][j]=0;
++depth_sol;
}
else{
M[i][j]=line[j]-'A'+1;
BRD[i][j]=M[i][j];
}
}
}
// find next vacant pos
for(i=0;i<16;i++)
for(j=0;j<16;j++)
if(!M[i][j]) goto haha;
haha:
State r(M,0,i,j);
r.show();
solve(r);
}
return 0;
}