Poj 2676 Sudoku dancing-links (DLX)

Source: Internet
Author: User

Question :.... Do I still use Sudoku?

First of all, the general solutions are brute-force searches .. So I learned the X Algorithm for Data Structure Optimization of dancing-links, or the DLX Algorithm for short.

Although the name of dancing-links is nice, it is actually a two-way cross linked list .. However, due to the various failures during debugging, the pointer is still looking at and worried (people who often debug the chain structure must have the same feeling), so they can only add pointers to and delete pointers in the debugging area, pointer for flying back and forth, that is, dancing-links...

The author of this algorithm is too talented to say ....

DLX algorithm is mainly to solve the problem of accurate coverage, specific practices see http://www.cnblogs.com/grenet/p/3145800.html

Here we need to transform the problem of data independence into a precise coverage problem.

First, what are the constraints of sudoku?

1. Only one number can be placed in a grid.

2. Only one number can be placed in each row.

3. Each column can contain only one number.

4. You can only put one number in each cell.

Therefore, each row has four constraints:

The 1.81 column indicates that a number has been placed in the grid.

Column 2.81 indicates that the number J has been placed in row I.

Column 3.81 indicates that column I has placed the number J

Column 4.81, which indicates that the number J has been placed in the ninth square lattice.

Then, each type of lattice may be added as a row.

Remember to use the * algorithm, otherwise it will die!

Each time you select a column with at least one to update, otherwise it will inevitably die.

Finally, the Code is pasted. It is more convenient to add a line at the bottom of the cross linked list.

<pre class="sh_cpp sh_sourceCode" style="background-color: white;font-size:14px; font-family: 'Courier New', Courier, monospace;"><pre name="code" class="html">#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std; struct abcd *stack[4000];int top; struct abcd{abcd *l,*r,*u,*d;int x,y,num;abcd *bottom;abcd(abcd *L,abcd *R,abcd *U,abcd *D,int X,int Y,int Num){l=L;if(L)L->r=this;r=R;if(R)R->l=this;u=U;if(U)U->d=this;d=D;if(D)D->u=this;x=X;y=Y;num=Num;bottom=d;if(bottom)bottom->x++;}void del(){if(l)l->r=r;if(r)r->l=l;if(u)u->d=d;if(d)d->u=u;if(bottom)bottom->x--;stack[++top]=this;}void restore(){if(l)l->r=this;if(r)r->l=this;if(u)u->d=this;if(d)d->u=this;if(bottom)bottom->x++;}}*head,*heads[400];int m,n,map[9][9];bool flag;void del_column(int pos){abcd *temp1,*temp2;for(temp1=heads[pos]->u;temp1;temp1=temp1->u){for(temp2=temp1->l;temp2;temp2=temp2->l)temp2->del();for(temp2=temp1->r;temp2;temp2=temp2->r)temp2->del();temp1->del();}heads[pos]->del();}inline void output(){int i,j;for(i=0;i<9;i++){for(j=0;j<9;j++)putchar(map[i][j]+'0');putchar('\n');}}void DLX(){if(!head->r){output();flag=1;return ;}int bottom=top,minnum=0x7fffffff;abcd *temp,*mintemp;for(temp=head->r;temp;temp=temp->r)if(temp->x<minnum)minnum=temp->x,mintemp=temp;for(temp=mintemp->u;temp;temp=temp->u){int x=temp->x,y=temp->y,num=temp->num;map[x][y]=num;del_column(x*9+y+1      );del_column(81+x*9+num      );del_column(162+y*9+num      );del_column(243+(x/3*3+y/3)*9+num  );DLX();if(flag)return ;while(top!=bottom)stack[top--]->restore();}}void add(int x,int y,int num){abcd *last=0x0,*temp;temp=heads[x*9+y+1      ],last=new abcd(last,0x0,temp->u,temp,x,y,num);temp=heads[81+x*9+num      ],last=new abcd(last,0x0,temp->u,temp,x,y,num);temp=heads[162+y*9+num      ],last=new abcd(last,0x0,temp->u,temp,x,y,num);temp=heads[243+(x/3*3+y/3)*9+num  ],last=new abcd(last,0x0,temp->u,temp,x,y,num);}int main(){int T,i,j,k,x;abcd *temp,*last; //freopen("sudoku.in","r",stdin);//freopen("sudoku.out","w",stdout);for(cin>>T;T;T--){head=new abcd(0x0,0x0,0x0,0x0,0,0,0);top=0;flag=0;for(i=1,last=head;i<=81*4;i++)last=new abcd(last,0x0,0x0,0x0,0,0,i),heads[i]=last;for(i=0;i<9;i++)for(j=0;j<9;j++)scanf("%1d",&map[i][j]);for(i=0;i<9;i++)for(j=0;j<9;j++)if(map[i][j])add(i,j,map[i][j]);elsefor(k=1;k<=9;k++)add(i,j,k);DLX();}}


 
 

Poj 2676 Sudoku dancing-links (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.