Poj3074-sudoku (Sudoku DLX)

Source: Internet
Author: User

Test instructions: Give a 9*9 matrix, some grids have been filled in, some are. Represents not filled in. Ask any set of solutions so that each row contains 1~9, each column contains 1~9, and each small rectangle (3*3) contains 1~9.

Resolution: Accurate coverage of DLX classic topics, each line represents the case to be filled, a total of 81*4 rows, the first 81 lines represent the line I J column number, the second 81 column represents the first row of the number K, the third 81 column

The number k is placed on behalf of Column J, and the fourth 81 line represents the first small rectangle in the second. For cases where the character is. Add 9 rows and one row for the case where the character is a number. And then just run, DLX, save the answer.

Output can be.

Code

#include <cstdio>#include<cstring>#include<string>#include<algorithm>using namespacestd;Const intinf=1e9+7;Const intms=Bayi*Ten;Const intmaxn=ms*4;intANS[MAXN];structdlx{intN,id; intL[MAXN],R[MAXN],U[MAXN],D[MAXN]; intc[maxn],s[maxn],loc[maxn][3]; intH[ms]; voidInitintnn=0) {n=nn;  for(intI=0; i<=n;i++) u[i]=d[i]=i,l[i]=i-1, r[i]=i+1; l[0]=n; r[n]=0; ID=N; memset (S),0,sizeof(S)); memset (H,-1,sizeof(H)); }    voidLink (intXintYintPxintPyintk) {++ID; D[id]=y; u[id]=U[y]; D[u[y]]=id; u[y]=ID; loc[id][0]=px,loc[id][1]=py,loc[id][2]=K; C[id]=y; s[y]++; if(h[x]==-1) h[x]=l[id]=r[id]=ID; Else        {            intA=H[x]; intb=R[a]; L[id]=a; r[a]=ID; R[id]=b; l[b]=ID; H[X]=ID; }    }    voidRemove (intc) {L[r[c]]=L[c]; R[L[C]]=R[c];  for(intI=d[c];i!=c;i=D[i]) for(intj=r[i];j!=i;j=R[j]) {U[d[j]]=U[j]; D[U[J]]=D[j]; S[C[J]]--; }    }    voidResume (intc) { for(intI=u[c];i!=c;i=U[i]) for(intj=r[i];j!=i;j=R[j]) {S[c[j]]++; U[D[J]]=J; D[U[J]]=J; } L[r[c]]=C; R[L[C]]=C; }    BOOLDfsintStep) {        if(step==Bayi)return true; if(r[0]==0)return false; intmin=inf,c=-1;  for(inti=r[0];i;i=R[i])if(Min>s[i]) {Min=s[i]; c=i;}        Remove (c);  for(intI=d[c];i!=c;i=D[i]) {Ans[step]=i;  for(intj=r[i];j!=i;j=R[j])            Remove (C[j]); if(Dfs (step+1))return true;  for(intj=l[i];j!=i;j=L[j])        Resume (C[j]);        } Resume (c); return false; }}DLX;intMain () {Chars[ -];  while(SCANF ("%s", S)! =EOF) {        if(s[0]=='e') Break; Dlx.init (Bayi*4); intk=0, r=0;  for(intx=0;x<9; x + +)             for(inty=0;y<9; y++)        {            Charch=s[k++]; inta,b,c,d; if(ch=='.')            {                 for(intI=1; i<=9; i++) {a=x*9+y+1; b=x*9+i+Bayi; C=y*9+i+Bayi+Bayi; intS= (x/3)*3+y/3; D=s*9+i+Bayi+Bayi+Bayi; ++R; Dlx.                    Link (R,a,x,y,i); Dlx.                    Link (R,b,x,y,i); Dlx.                    Link (R,c,x,y,i); Dlx.                Link (R,d,x,y,i); }            }            Else            {                inti=ch-'0'; A=x*9+y+1; b=x*9+i+Bayi; C=y*9+i+Bayi+Bayi; intS= (x/3)*3+y/3; D=s*9+i+Bayi+Bayi+Bayi; ++R; Dlx.                Link (R,a,x,y,i); Dlx.                Link (R,b,x,y,i); Dlx.                Link (R,c,x,y,i); Dlx.            Link (R,d,x,y,i); }} Dlx.dfs (0); intres[Ten][Ten];  for(intI=0;i<Bayi; i++)        {            intA=Ans[i]; intx=dlx.loc[a][0],y=dlx.loc[a][1],k=dlx.loc[a][2]; Res[x][y]=K; }         for(intI=0;i<9; i++)             for(intj=0;j<9; j + +) printf ("%d", Res[i][j]); printf ("\ n"); }    return 0;}
View Code

Poj3074-sudoku (Sudoku DLX)

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.