HDU 4069 (dancing links Sudoku)

Source: Internet
Author: User

Squigugly Sudoku

Time Limit: 4000/2000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 81 accepted submission (s): 18

Problem descriptiontoday we play a squigugly 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.

Inputthe 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.

Outputfor 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

3144 18 112 208 80 25 54 144 48135 38 147 80 121 128 97 130 32137 32 160 144 114 167 208 0 32192 100 160 160 208 96 183 192 101209 80 39 192 86 48 136 80 114152 48 226 144 112 160 160 149 48128 0 112 166 215 96 160 128 41128 39 153 32 209 80 101 136 35192 96 200 67 80 112 208 68 96 144 48 144 81 81 16 53 144 48128 96 224 144 48 128 103 128 38163 208 80 0 37 224 209 0 32135 48 176 192 64 112 176 192 104192 101 128 89 80 82 32 150 48149 48 224 208 16 48 224 192 33128 0 114 176 135 0 80 112 169137 32 148 32 192 96 176 144 32192 96 193 64 80 80 96 192 96144 88 48 217 16 16 80 112 176224 176 129 48 128 40 208 16 37145 32 128 96 196 96 176 136 32192 32 227 176 144 80 96 192 32176 192 80 98 160 145 80 48 224128 48 144 80 96 224 183 128 48128 36 224 144 51 144 32 128 105131 64 112 136 32 192 36 224 176224 208 80 64 64 116 192 83 96
 

Sample output

Case 1:521439678763895124984527361346182795157964832812743956235678419479216583698351247Case 2:No solutionCase 3:Multiple Solutions
 

Sourcethe 36th ACM/ICPC Asia Regional
Fuzhou site -- online contest

Recommendlcy question: http://acm.hdu.edu.cn/showproblem.php? PID = 4069 analysis: This is a Sudoku. It can obviously be done using dancing links. It is just a template question. If you don't want to learn it, you can learn it. Interesting algorithms ~~~ Code:

#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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.