Bzoj 1501 noi2005 smart beads game dancing-links (DLX)

Source: Internet
Author: User

Given a 10x10 triangle board and 12 kinds of parts, each part can only be placed once, and can be rotated and flipped. Some parts have been placed on the top, and a solution is proposed, place 12 parts on the board without Overlap

First of all, this question is about DLX, but it is really disgusting to create a graph. We need to enumerate up to eight orientation positions for each part. At first, I wanted to process all the code, but later I found that I could not do it.

So I chose to input all 60 orientations of 12 parts in a table, and selected the leftmost vertex in the first row as the base point to obtain the relative position of each vertex on the base point, then, the position of the enumeration base point is shown. If all vertices are added to the graph

A total of 60*5*2 tables have been played for 40 minutes, and the keyboard has been knocked out. The system almost consumes 0.0 Of the total N in the body (c6h12o6) before dinner at night.

After writing the script, you will be able to adjust it a little later ~ Even wa once, forget to output no solution .....

The code is 6.8kb in total, which occupies a lot of space. However, by comparing the code of the experts, I found that all of them are short. These three questions are really disgusting.

In addition, there are a total of 32288 ways to put this question... I'm bored...

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;int top;struct abcd *stack[40000];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);void del();void output();void restore();}*head,*heads[100];const int start[]={0,0,4,6,14,15,19,27,31,39,47,48,52,60};const int size[]={0,3,4,4,4,5,5,5,5,5,5,5,5};const int table[60][5][2]={{ {0,0} , {1,0} , {1,1} , {0,0} , {0,0} },{ {0,0} , {1,-1} , {1,0} , {0,0} , {0,0} },{ {0,0} , {0,1} , {1,0} , {0,0} , {0,0} },{ {0,0} , {0,1} , {1,1} , {0,0} , {0,0} },{ {0,0} , {0,1} , {0,2} , {0,3} , {0,0} },{ {0,0} , {1,0} , {2,0} , {3,0} , {0,0} },{ {0,0} , {0,1} , {0,2} , {1,0} , {0,0} },{ {0,0} , {0,1} , {1,1} , {2,1} , {0,0} },{ {0,0} , {1,-2} , {1,-1} , {1,0} , {0,0} },{ {0,0} , {1,0} , {2,0} , {2,1} , {0,0} },{ {0,0} , {0,1} , {1,0} , {2,0} , {0,0} },{ {0,0} , {0,1} , {0,2} , {1,2} , {0,0} },{ {0,0} , {1,0} , {2,0} , {2,-1} , {0,0} },{ {0,0} , {1,0} , {1,1} , {1,2} , {0,0} },{ {0,0} , {0,1} , {1,0} , {1,1} , {0,0} },{ {0,0} , {1,0} , {2,0} , {2,1} , {2,2} },{ {0,0} , {0,1} , {0,2} , {1,0} , {2,0} },{ {0,0} , {0,1} , {0,2} , {1,2} , {2,2} },{ {0,0} , {1,0} , {2,-2} , {2,-1} , {2,0} },{ {0,0} , {1,-2} , {1,-1} , {1,0} , {1,1} },{ {0,0} , {1,0} , {2,0} , {2,1} , {3,0} },{ {0,0} , {0,1} , {0,2} , {0,3} , {1,1} },{ {0,0} , {1,-1} , {1,0} , {2,0} , {3,0} },{ {0,0} , {1,0} , {1,1} , {2,0} , {3,0} },{ {0,0} , {0,1} , {0,2} , {0,3} , {1,2} },{ {0,0} , {1,0} , {2,-1} , {2,0} , {3,0} },{ {0,0} , {1,-1} , {1,0} , {1,1} , {1,2} },{ {0,0} , {0,2} , {1,0} , {1,1} , {1,2} },{ {0,0} , {0,1} , {1,0} , {2,0} , {2,1} },{ {0,0} , {0,1} , {0,2} , {1,0} , {1,2} },{ {0,0} , {0,1} , {1,1} , {2,0} , {2,1} },{ {0,0} , {0,1} , {1,0} , {1,1} , {2,1} },{ {0,0} , {0,1} , {1,-1} , {1,0} , {1,1} },{ {0,0} , {1,0} , {1,1} , {2,0} , {2,1} },{ {0,0} , {0,1} , {0,2} , {1,0} , {1,1} },{ {0,0} , {0,1} , {0,2} , {1,1} , {1,2} },{ {0,0} , {1,-1} , {1,0} , {2,-1} , {2,0} },{ {0,0} , {0,1} , {1,0} , {1,1} , {1,2} },{ {0,0} , {0,1} , {1,0} , {1,1} , {2,0} },{ {0,0} , {1,-1} , {1,0} , {2,-1} , {3,-1} },{ {0,0} , {0,1} , {0,2} , {1,2} , {1,3} },{ {0,0} , {1,0} , {2,-1} , {2,0} , {3,-1} },{ {0,0} , {0,1} , {1,1} , {1,2} , {1,3} },{ {0,0} , {0,1} , {1,-2} , {1,-1} , {1,0} },{ {0,0} , {1,0} , {2,0} , {2,1} , {3,1} },{ {0,0} , {0,1} , {0,2} , {1,-1} , {1,0} },{ {0,0} , {1,0} , {1,1} , {2,1} , {3,1} },{ {0,0} , {1,-1} , {1,0} , {1,1} , {2,0} },{ {0,0} , {1,0} , {1,1} , {2,1} , {2,2} },{ {0,0} , {0,1} , {1,-1} , {1,0} , {2,-1} },{ {0,0} , {0,1} , {1,1} , {1,2} , {2,2} },{ {0,0} , {1,-1} , {1,0} , {2,-2} , {2,-1} },{ {0,0} , {1,-3} , {1,-2} , {1,-1} , {1,0} },{ {0,0} , {1,0} , {2,0} , {3,0} , {3,1} },{ {0,0} , {0,1} , {0,2} , {0,3} , {1,0} },{ {0,0} , {0,1} , {1,1} , {2,1} , {3,1} },{ {0,0} , {0,1} , {1,0} , {2,0} , {3,0} },{ {0,0} , {0,1} , {0,2} , {0,3} , {1,3} },{ {0,0} , {1,0} , {2,0} , {3,-1} , {3,0} },{ {0,0} , {1,0} , {1,1} , {1,2} , {1,3} }};int map[11][11],repos[11][11],cnt=12;bool appeared[13],mark[11][11];void DLX();void output();void find(int p);inline char getnum();void del_row(abcd *pos);void add(int p,int pos);void del_column(abcd *pos);int main(){int i,j;abcd *last;head=new abcd(0x0,0x0,0x0,0x0,0,0,0);for(last=head,i=1;i<=67;i++)last=new abcd(last,0x0,0x0,0x0,0,0,0),heads[i]=last;for(i=1;i<=10;i++)for(j=1;j<=i;j++)repos[i][j]=++cnt,map[i][j]=getnum();for(i=1;i<=12;i++)if(!appeared[i])for(j=start[i];j!=start[i+1];j++)add(i,j);elsefind(i);DLX();puts("No solution");return 0;}abcd :: 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 abcd :: 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 abcd :: 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++;}void output(){int i,j;for(i=1;i<=10;i++){for(j=1;j<=i;j++)putchar(map[i][j]+'A'-1);putchar('\n');}}void del_column(abcd *pos){abcd *temp1,*temp2;for(temp1=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();}pos->del();}void del_row(abcd *pos){if(!pos)return ;del_row(pos->r);del_column(pos->bottom);int x=pos->x,y=pos->y,num=pos->num;map[x][y]=num;}void DLX(){if(!head->r){output();exit(0);}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){for(mintemp=temp;mintemp->l;mintemp=mintemp->l);del_row(mintemp);DLX();while(top!=bottom)stack[top--]->restore();}}inline char getnum(){char c;do c=getchar(); while(c==' '||c=='\n'||c=='\r');if(c=='.')return 0;appeared[c-'A'+1]=1;return c-'A'+1;}void add(int p,int pos){int i,j,k,x,y;for(i=1;i<=10;i++)for(j=1;j<=i;j++){for(k=0;k<size[p];k++){x=i+table[pos][k][0];y=j+table[pos][k][1];if(x>0&&y>0&&x<=10&&y<=x);elsebreak;}if(k==size[p]){abcd *last,*temp;temp=heads[ p ];last = new abcd(0x0,0x0,temp->u,temp,0,0,0);for(k=0;k<size[p];k++){x=i+table[pos][k][0];y=j+table[pos][k][1];temp=heads[ repos[x][y] ];last = new abcd(last,0x0,temp->u,temp,x,y,p);}}}}void find(int p){int i,j;abcd *last,*temp;temp=heads[ p ];last = new abcd(0x0,0x0,temp->u,temp,0,0,0);for(i=1;i<=10;i++)for(j=1;j<=i;j++)if(map[i][j]==p)temp=heads[ repos[i][j] ],last = new abcd(last,0x0,temp->u,temp,i,j,p);}


Bzoj 1501 noi2005 smart beads game 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.