"DLX algorithm" poj2676 Sudoku

Source: Internet
Author: User

The DLX algorithm solves the exact overlay problem template. You can see petition on the court.

#include <cstdio> #include <cstring> #include <vector>using namespace Std;const int sub[10][10]={{ 0,0,0,0,0,0,0,0,0,0},{0,1,1,1,2,2,2,3,3,3},{0,1,1,1,2,2,2,3,3,3},{0,1,1,1,2,2,2,3,3,3},{0,4,4,4,5,5,5,6,6,6},{ 0,4,4,4,5,5,5,6,6,6},{0,4,4,4,5,5,5,6,6,6},{0,7,7,7,8,8,8,9,9,9},{0,7,7,7,8,8,8,9,9,9},{0,7,7,7,8,8,8,9,9,9}}; int hlb[12],hub[12],llb[12],lub[12];const int maxn=4*9*9+5;const int Maxr=9*9*9+5;const int maxnode=9*9*9*4+maxn+5;// Ðð±àºå´ó1¿ªê¼£¬áð±àºåîª1~n£¬½áµã0êç±íí ½áµã£»½áµã1~nêç¸÷á𶥲¿µäðéäâ½áµãstruct DLX{int n,sz;//áðêý£¬½áµãxüêýint S [Maxn];//¸÷áð½áµãêýint row[maxnode],col[maxnode];//¸÷½áµãëùôúµäððáð±àºåint l[maxnode],r[maxnode],u[maxnode],d[ Maxnode];int ansd,ans[maxr];//½âvoid init (int n) {//nêçáðêýthis->n=n;for (int i=0;i<=n;++i) {U[i]=i;D[i]=i; L[i]=i-1; r[i]=i+1;} r[n]=0; L[0]=n;sz=n+1;memset (s,0,sizeof (S));} void AddRow (int r,vector<int> columns) {int first=sz;for (int i=0;i<columns.size (); ++i) {int c=columns[i]; L[sz]=sz-1; R[sz]=sz+1;d[sz]=c; U[sz]=u[C];D [U[c]]=sz; U[c]=sz;row[sz]=r;col[sz]=c;++s[c];++sz;} R[sz-1]=first; L[first]=sz-1;} ˳xåá´±ía£¬±éàú³ýsíâµäæäëûôªëø #define FOR (i,a,s) for (int i=a[s];i!=s;i=a[i]) void remove (int c) {l[r[c]]=l[c]; R[L[C]]=R[C]; for (I,d,c) {for (j,r,i) {u[d[j]]=u[j];D [u[j]]=d[j];--s[col[j]];}} void restore (int c) {for (i,u,c) {for (j,l,i) {++s[col[j]]; U[d[j]]=j;d[u[j]]=j;}} L[r[c]]=c; R[l[c]]=c;} BOOL Dfs (int d) {//printf ("%d", d); if (r[0]==0) {//õòµ½½âansd=d;//¼çâ¼½âµä³¤¶èreturn 1;} õò½áµãêýxîð¡µäáðc int C=r[0];//µúò»¸öî´é¾³ýµäáðfor (i,r,0) {if (S[i]<s[c]) {c=i;}} Remove (c);//é¾³ýµúcáðfor (i,d,c) {//óã½áµãiëùôúð𸲸çµúcáðans[d]=row[i]; for (j,r,i) {remove (col[j]);//é¾³ý½áµãiëùôúððäü¸²¸çµäëùóðæäëûáð}if (Dfs (d+1)) {return 1;} for (j,l,i) {restore (col[j]);//»ö¸´½áµãiëùôúððäü¸²¸çµäæäëûëùóðáð}}restore (c);//»ö¸´µúcáðreturn 0;} BOOL Solve (vector<int>& v) {v.clear (); if (!dfs (0)) {return 0;} for (int i=0;i<ansd;++i) {v.push_back (ans[i]);} return 1;}} Dlx;char s[12][12];int a[12][12];int Mah[1005],mal[1005],mav[1005];int encode(int a,int b,int c) {return a*81+b*9+c+1;}    void decode (int code,int &a,int &b,int &c) {--code;    c=code%9;code/=9;    b=code%9;code/=9;  A=code;} int main () {int Zu;//freopen ("poj2676.in", "R", stdin),//freopen ("Poj2676.out", "w", stdout), memset (Hlb,0x7f,sizeof ( HLB)); Memset (Llb,0x7f,sizeof (LLB)); for (int. i=1;i<=9;++i) {for (int j=1;j<=9;++j) {hlb[sub[i][j]]=min (hlb[sub[ I][j]],i); Hub[sub[i][j]]=max (hub[sub[i][j]],i); Llb[sub[i][j]]=min (llb[sub[i][j]],j); Lub[sub[i][j]]=max (lub[sub I [J]],j);}} scanf ("%d", &zu), for (; zu;--zu) {for (int i=1;i<=9;++i) {scanf ("%s", s[i]+1);} int hang=0,lie=0;for (int i=1;i<=9;++i) {for (int j=1;j<=9;++j) {a[i][j]=s[i][j]-' 0 ';}} Dlx.init (9*9*4); for (int. i=1;i<=9;++i) {for (int. j=1;j<=9;++j) {for (int k=1;k<=9;++k) {if (!a[i][j] | | a[i][j]== k) {vector<int> Columns;columns.push_back (Encode (0,i-1,j-1)); Columns.push_back (Encode (1,i-1,k-1)); Columns.push_back (Encode (2,j-1,k-1)), Columns.push_back (Encode (3,sub[i][j]-1,k-1));d Lx.addrow (Encode (i-1,j-1,k-1), columns);}}} Vector<int> ans;dlx.solve (ANS); for (int i=0;i<ans.size (); ++i) {int r,c,v;decode (ANS[I],R,C,V); a[r+1][c+1]= v+1;} for (int i=1;i<=9;++i) {for (int j=1;j<9;++j) {printf ("%d", A[i][j]);} printf ("%d\n", A[i][9]);}} return 0;}

DLX algorithm poj2676 Sudoku

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.