Squiggly Sudoku
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 81 Accepted Submission(s): 18
Problem Description
Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each column, each row, and each of the nine Connecting-sub-grids that compose the grid contains all of the digits from 1 to 9.
Left figure is the puzzle and right figure is one solution.
Now, give you the information of the puzzle, please tell me is there no solution or multiple solution or one solution.
Input
The first line is a number T(1<=T<=2500), represents the number of case. The next T blocks follow each indicates a case.
Each case contains nine lines, Each line contains nine integers.
Each module number tells the information of the gird and is the sum of up to five integers:
0~9: '0' means this gird is empty, '1' - '9' means the gird is already filled in.
16: wall to the up
32: wall to the right
64: wall to the down
128: wall to the left
I promise there must be nine Connecting-sub-grids, and each contains nine girds.
Output
For each case, if there are Multiple Solutions or no solution just output "Multiple Solutions" or "No solution". Else output the exclusive solution.(as shown in the sample output)
Sample Input
3
144 18 112 208 80 25 54 144 48
135 38 147 80 121 128 97 130 32
137 32 160 144 114 167 208 0 32
192 100 160 160 208 96 183 192 101
209 80 39 192 86 48 136 80 114
152 48 226 144 112 160 160 149 48
128 0 112 166 215 96 160 128 41
128 39 153 32 209 80 101 136 35
192 96 200 67 80 112 208 68 96
144 48 144 81 81 16 53 144 48
128 96 224 144 48 128 103 128 38
163 208 80 0 37 224 209 0 32
135 48 176 192 64 112 176 192 104
192 101 128 89 80 82 32 150 48
149 48 224 208 16 48 224 192 33
128 0 114 176 135 0 80 112 169
137 32 148 32 192 96 176 144 32
192 96 193 64 80 80 96 192 96
144 88 48 217 16 16 80 112 176
224 176 129 48 128 40 208 16 37
145 32 128 96 196 96 176 136 32
192 32 227 176 144 80 96 192 32
176 192 80 98 160 145 80 48 224
128 48 144 80 96 224 183 128 48
128 36 224 144 51 144 32 128 105
131 64 112 136 32 192 36 224 176
224 208 80 64 64 116 192 83 96
Sample Output
Case 1:
521439678
763895124
984527361
346182795
157964832
812743956
235678419
479216583
698351247
Case 2:
No solution
Case 3:
Multiple Solutions
Source
The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest
Recommend
lcy
题目: http://acm.hdu.edu.cn/showproblem.php?pid=4069
分析:这题数独,明显可以用Dancing Links来做,就是个模板题了,不会的可以去学一学,有趣的算法~~~
代码:
#include<cstdio>
#include<cstring>
using namespace std;
const int dx[]={-1,1,0,0};
const int dy[]={0,0,-1,1};
const int N=9;
const int mm=N*N*N*(N*N*4)+N;
const int mn=N*N*N+N;
int U[mm],D[mm],L[mm],R[mm],C[mm],X[mm];
int H[mn],S[mn],Q[mn],Qx[mn],Qy[mn];
bool v[mn];
int map[mn],out[mn];
int g[22][22];
int size,ans;
void prepare(int r,int c)
{
for(int i=0;i<=c;++i)
{
S[i]=0;
U[i]=D[i]=i;
L[i+1]=i;
R[i]=i+1;
}
R[size=c]=0;
while(r)H[r--]=-1;
}
void remove(int c)
{
L[R[c]]=L[c],R[L[c]]=R[c];
for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
U[D[j]]=U[j],D[U[j]]=D[j],--S[C[j]];
}
void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
++S[C[U[D[j]]=D[U[j]]=j]];
L[R[c]]=R[L[c]]=c;
}
bool Dance(int k)
{
if(ans>1)return 1;
if(!R[0])
{
++ans;
for(int i=0;i<k;++i)out[(X[Q[i]]-1)/9+1]=(X[Q[i]]-1)%9+1;
return 0;
}
int i,j,c,tmp=mm;
for(i=R[0];i;i=R[i])
if(S[i]<tmp)tmp=S[c=i];
remove(c);
for(i=D[c];i!=c;i=D[i])
{
Q[k]=i;
for(j=R[i];j!=i;j=R[j])remove(C[j]);
if(Dance(k+1))return 1;
for(j=L[i];j!=i;j=L[j])resume(C[j]);
}
resume(c);
return 0;
}
void Link(int r,int c)
{
++S[C[++size]=c];
X[size]=r;
D[size]=D[c];
U[D[c]]=size;
U[size]=c;
D[c]=size;
if(H[r]<0)H[r]=L[size]=R[size]=size;
else
{
R[size]=R[H[r]];
L[R[H[r]]]=size;
L[size]=H[r];
R[H[r]]=size;
}
}
void place(int &r,int &c1,int &c2,int &c3,int &c4,int i,int j,int k)
{
r=(i*N+j)*N+k,c1=i*N+j+1,c2=N*N+i*N+k,c3=N*N*2+j*N+k,c4=N*N*3+(g[(i+1)*2][(j+1)*2]-1)*N+k;
}
void fill(int x,int y,int flag)
{
int i,l,r=1;
Qx[0]=x,Qy[0]=y;
for(l=0;l<r;++l)
for(i=0;i<4;++i)
if(!g[Qx[l]+dx[i]][Qy[l]+dy[i]])
{
x=Qx[l]+dx[i]*2;
y=Qy[l]+dy[i]*2;
if(!g[x][y])
{
g[x][y]=flag;
Qx[r]=x,Qy[r]=y,++r;
}
}
}
int main()
{
int i,j,k,r,c1,c2,c3,c4,t,cas=0;
scanf("%d",&t);
while(t--)
{
memset(g,0,sizeof(g));
for(r=0,i=1;i<=N;++i)
for(j=1;j<=N;++j,++r)
{
scanf("%d",&k);
g[i*2][j*2]=0;
if(k>=128)g[i*2][j*2-1]=1,k-=128;
if(k>=64)g[i*2+1][j*2]=1,k-=64;
if(k>=32)g[i*2][j*2+1]=1,k-=32;
if(k>=16)g[i*2-1][j*2]=1,k-=16;
map[r]=k;
}
for(k=0,i=1;i<=N;++i)
for(j=1;j<=N;++j)
if(!g[i*2][j*2])fill(i*2,j*2,++k);
prepare(mn,N*N*4);
for(i=1;i<=mn;++i)v[i]=0;
for(k=0,i=0;i<9;++i)
for(j=0;j<9;++j,++k)
if(map[k])
{
place(r,c1,c2,c3,c4,i,j,map[k]);
Link(r,c1),Link(r,c2),Link(r,c3),Link(r,c4);
v[c2]=v[c3]=v[c4]=1;
}
for(i=0;i<9;++i)
for(j=0;j<9;++j)
for(k=1;k<=9;++k)
{
place(r,c1,c2,c3,c4,i,j,k);
if(v[c2]||v[c3]||v[c4])continue;
Link(r,c1),Link(r,c2),Link(r,c3),Link(r,c4);
}
Dance(ans=0);
printf("Case %d:\n",++cas);
if(ans>1)puts("Multiple Solutions");
else if(!ans)puts("No solution");
else
{
for(i=1;i<=N*N;++i)
{
printf("%d",out[i]);
if((i%N)==0)puts("");
}
}
}
return 0;
}