Zoj 3122 (precise coverage of sudoku dancing links)

Source: Internet
Author: User


Sudoku

Time Limit: 10 seconds memory limit: 32768 KB

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters fromAToP(The first 16 capital letters of the English alphabet), as shown in Figure 1a. The game is to fill
All the empty grid cells with letters fromAToPSuch that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned
Above and guarantees a unique solution.


Write a Sudoku playing program that reads data sets from a text file. Each Data Set encodes a grid and contains 16 strings on 16 consecutive lines as shown in Figure 2.ITh string stands forITh
Line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,...,P,-}, Where-(Minus) designates empty
Grid cells. the data sets are separated by Single Empty lines and terminate with an end of file. the program prints the solution of the input encoded grids in the same format and order as used for input.

Sample Input

--A----C-----O-I-J--A-B-P-CGF-H---D--F-I-E----P--G-EL-H----M-J------E----C--G----I--K-GA-B---E-JD-GP--J-F----A---E---C-B--DP--O-E--F-M--D--L-K-A-C--------O-I-L-H-P-C--F-A--B------G-OD---J----HK---J----H-A-P-L--B--P--E--K--A--H--B--K--FI-C----F---C--D--H-N-

Sample output

FPAHMJECNLBDKOGIOJMIANBDPKCGFLHELNDKGFOIJEAHMBPCBGCELKHPOFIMAJDNMFHBELPOACKJGNIDCILNKDGAHBMOPEFJDOGPIHJMFNLECAKBJEKAFCNBGIDPLHOMEBOFPMIJDGHLNKCANCJDHBAEKMOFIGLPHMPLCGKFIAENBDJOAKIGNODLBPJCEFMHKDEMJIFNCHGAOPBLGLBCDPMHEONKJIAFPHNOBALKMJFIDCEGIAFJOECGLDPBHMNK

Source:Southeastern European Regional 2006

Question: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 3038

Analysis: there is a problem with this example. I changed it. Note that empty rows are output every time the solution is output, and empty rows are not used in the last group...

Code:

#include<cstdio>using namespace std;const int N=16;const int mm=N*N*N*4+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];bool v[mn];char map[mn];int size;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(!R[0])    {        for(int i=0;i<k;++i)map[(X[Q[i]]-1)/N+1]=(X[Q[i]]-1)%N+'A';        for(int i=1;i<=N*N;++i)        {            putchar(map[i]);            if((i%N)==0)puts("");        }        return 1;    }    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+((i/4)*4+(j/4))*N+k;}bool init(){    char c;    while(((c=getchar())<'A'||c>'P')&&c!='-'&&c!=EOF);    if(c==EOF)return 0;    for(int i=0;i<N*N;++i)    {        if(i)while(((c=getchar())<'A'||c>'P')&&c!='-');        map[i]=c;    }    return 1;}int main(){    int i,j,k,r,c1,c2,c3,c4,T=0;    while(init())    {        if(T++)puts("");        prepare(mn,N*N*4);        for(i=1;i<=mn;++i)v[i]=0;        for(k=i=0;i<N;++i)            for(j=0;j<N;++j,++k)            if(map[k]!='-')            {                place(r,c1,c2,c3,c4,i,j,map[k]-'A'+1);                Link(r,c1),Link(r,c2),Link(r,c3),Link(r,c4);                v[c2]=v[c3]=v[c4]=1;            }        for(i=0;i<N;++i)            for(j=0;j<N;++j)                for(k=1;k<=N;++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(0);    }    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.