| ||||||||||
Squiggly SudokuTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1478 Accepted Submission(s): 613
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
Sample Output
|
思路:跟普通数独差不多,只需要先进行一遍dfs,对数独进行分块,然后就是普通的数独了
写的太恶心了
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=9*9*9+10;
const int maxm=9*9*4+10;
const int maxnode=maxn*4+maxm+10;
const int N=20;
const int SLOT=0;
const int ROW=1;
const int COL=2;
const int SUB=3;
int id[N][N],a[N][N],cnt;
char g[maxnode];
int encode(int a,int b,int c)
{
return a*81+b*9+c;
}
void decode(int code,int &a,int &b,int &c)
{
code--;
c=code%9;code/=9;
b=code%9;code/=9;
a=code;
}
struct DLX
{
int n,m,size,num;
int U[maxnode],D[maxnode],L[maxnode],R[maxnode];
int H[maxn],S[maxm];
int row[maxnode],col[maxnode];
int ansd,ans[maxnode];
void init(int N,int M)
{
n=N,m=M;
for(int i=0; i<=m; i++)
{
U[i]=D[i]=i;
L[i]=i-1;
R[i]=i+1;
}
L[0]=m;
R[m]=0;
size=m;
memset(S,0,sizeof(S));
memset(H,-1,sizeof(H));
}
void Link(int r,int c)
{
++S[col[++size]=c];
row[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 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[col[j]];
}
}
}
void restore(int c)
{
for(int i=U[c]; i!=c; i=U[i])
{
for(int j=L[i]; j!=i; j=L[j])
{
U[D[j]]=D[U[j]]=j;
++S[col[j]];
}
}
L[R[c]]=R[L[c]]=c;
}
void Dance(int d)
{
if(cnt>1)return;
if(R[0]==0)
{
ansd=d;
for(int i=0; i<d; i++)
{
int r,c,v;
decode(ans[i],r,c,v);
a[r][c]=v+1;
}
cnt++;
return;
}
int c=R[0];
for(int i=R[0]; i; i=R[i])
if(S[i]<S[c])c=i;
remove(c);
for(int i=D[c]; i!=c; i=D[i])
{
ans[d]=row[i];
for(int j=R[i]; j!=i; j=R[j])remove(col[j]);
Dance(d+1);
if(cnt>1)return;
for(int j=L[i]; j!=i; j=L[j])restore(col[j]);
}
restore(c);
}
} dlx;
void bfs(int sx,int sy,int index)
{
queue<pair<int,int> > q;
q.push(make_pair(sx,sy));
id[sx][sy]=index;
while(!q.empty())
{
pair<int,int> tmp=q.front();q.pop();
int x=tmp.first,y=tmp.second;
if(a[x][y]<128&&y-1>=0&&id[x][y-1]==-1)
q.push(make_pair(x,y-1)),id[x][y-1]=index;
else if(a[x][y]>=128)a[x][y]-=128;
if(a[x][y]<64&&x+1<9&&id[x+1][y]==-1)
q.push(make_pair(x+1,y)),id[x+1][y]=index;
else if(a[x][y]>=64)a[x][y]-=64;
if(a[x][y]<32&&y+1<9&&id[x][y+1]==-1)
q.push(make_pair(x,y+1)),id[x][y+1]=index;
else if(a[x][y]>=32)a[x][y]-=32;
if(a[x][y]<16&&x-1>=0&&id[x-1][y]==-1)
q.push(make_pair(x-1,y)),id[x-1][y]=index;
else if(a[x][y]>=16)a[x][y]-=16;
}
}
int main()
{
int T,cas=1;
scanf("%d",&T);
while(T--)
{
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)scanf("%d",&a[i][j]);
memset(id,-1,sizeof(id));
int tmp=0;
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
if(id[i][j]==-1)
bfs(i,j,++tmp);
dlx.init(9*9*9,9*9*4);
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
for(int k=1; k<=9; k++)
{
if(a[i][j]%16==0||a[i][j]%16==k)
{
int row=encode(i,j,k);
dlx.Link(row,encode(SLOT,i,j)+1);
dlx.Link(row,encode(ROW,i,k));
dlx.Link(row,encode(COL,j,k));
dlx.Link(row,encode(SUB,id[i][j]-1,k));
}
}
cnt=0;
dlx.Dance(0);
printf("Case %d:\n",cas++);
if(cnt==0)printf("No solution\n");
else
{
if(cnt>1)printf("Multiple Solutions\n");
else
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
printf("%d",a[i][j]);
printf("\n");
}
}
}
}
return 0;
}