Test instructions
First give you the n block (2*1 or 1*2), number and position
and give you 9 individual plates, 1~9, separate positions.
Then ask how you put the other board (1*2 or 2*1) making it a Sudoku
Note that for the board, the number that is contained is unique.
That is, the plates with the numbers 1 and 2 have only 1 pieces.
Ideas:
Feel too bad, write a super trouble to build the map
The column is 5*9*9.
The front 4*9*9 is just like the ordinary Sudoku.
The 9*9 in the back is the number of the plates that are represented on the board, which marks the number pairs (i,j).
Of course you need to add special i==j to these boards.
Then the line is the first n blocks and 9 blocks, and there are 9 special
The remainder is enumerated for the number pairs that are not present
Then enumerate the positions that can be placed
And then the release vessel is vertical.
This builds the diagram.
Code:
#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "using namespace std; #define N 14010* (4*9*9+9*9) #define M 14010struct dlx{int n,m,c; int u[n],d[n],l[n],r[n],row[n],col[n]; int h[m],s[m],cnt,ans[m]; void init (int _n,int _m) {n=_n; M=_m; for (int i=0; i<=m; i++) {u[i]=d[i]=i; L[i]= (i==0?m:i-1); R[i]= (i==m?0:i+1); s[i]=0; } c=m; for (int i=1; i<=n; i++) h[i]=-1; } void link (int x,int y) {C + +; Row[c]=x; Col[c]=y; s[y]++; U[c]=u[y]; D[c]=y; D[u[y]]=c; U[y]=c; if (h[x]==-1) h[x]=l[c]=r[c]=c; else {l[c]=l[h[x]]; R[C]=H[X]; R[l[h[x]]]=c; L[h[x]]=c; }} void del (int x) {r[l[x]]=r[x]; L[R[X]]=L[X]; for (int i=d[x]; i!=x; I=d[i]){for (int j=r[i]; j!=i; J=r[j]) {u[d[j]]=u[j]; D[U[J]]=D[J]; s[col[j]]--; }}}} void rec (int x) {for (int. i=u[x]; i!=x; I=u[i]) {for (int j=l[i]; j!=i; j=l [j]) {u[d[j]]=j; D[u[j]]=j; s[col[j]]++; }} r[l[x]]=x; L[r[x]]=x; } int Dance (int x) {if (r[0]==0) {cnt=x; return 1; } int now=r[0]; for (int i=r[0]; i!=0; I=r[i]) {if (S[i]<s[now]) now=i; } del (now); for (int i=d[now]; i!=now; I=d[i]) {ans[x]=row[i]; for (int j=r[i]; j!=i; J=r[j] del (col[j]); if (dance (x+1)) return 1; for (int j=l[i]; j!=i; j=l[j]) rec (col[j]); } rec (now); return 0; }} dlx;struct node{int A, b; int x,y,f;} Kx[m];void Getplace (int i,int j,int k,int &a,int &b,int &c) {a=81+ (i-1) *9+k; b=81*2+ (j-1) *9+k; c=81*3+ ((i-1)/3*3+ (j-1)/3) *9+k;} int main () {int n,cas=1; while (scanf ("%d", &n), n) {int mark[12][12],mp[12][12],//mark marks which pairs appear memset (MP); memset (Mark,0,sizeof (Mark)); int cnt=0; Dlx.init (14000,5*9*9); for (int i=1; i<=n; i++)//start with n boards {cnt++; int x,tep,d1,d2; Char y[10]; scanf ("%d%s", &x,y); D1=x; mp[y[0]-' A ' +1][y[1]-' 0 ']=x; tep= (y[0]-' A ') *9+y[1]-' 0 '; int a,b,c; Getplace (y[0]-' A ' +1,y[1]-' 0 ', x,a,b,c); Dlx.link (Cnt,tep); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); scanf ("%d%s", &x,y); D2=x; mp[y[0]-' A ' +1][y[1]-' 0 ']=x; tep= (y[0]-' A ') *9+y[1]-' 0 '; Getplace (y[0]-' A ' +1,y[1]-' 0 ', x,a,b,c); Dlx.link (cNT,TEP); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); Mark[d1][d2]=mark[d2][d1]=1; Dlx.link (cnt,81*4+ (d1-1) *9+d2); Dlx.link (cnt,81*4+ (d2-1) *9+d1); } for (int i=1; i<=9; i++)//Join single board {cnt++; Char y[12]; scanf ("%s", y); mp[y[0]-' A ' +1][y[1]-' 0 ']=i; int tep= (y[0]-' A ') *9+y[1]-' 0 '; int a,b,c; Getplace (y[0]-' A ' +1,y[1]-' 0 ', i,a,b,c); Dlx.link (Cnt,tep); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); } for (int i=1; i<=9; i++)//Add special board {cnt++; Dlx.link (cnt,81*4+ (i-1) *9+i); } for (int i=1, i<=9; i++) {for (int j=1; j<=9; J + +) {if (I==J) CO Ntinue; if (Mark[i][j]) continue; for (int xx=1; xx<=9; xx++)//cross-put { for (int yy=1; yy<=8; yy++) {cnt++; Kx[cnt].a=i; Kx[cnt].b=j; kx[cnt].f=0; Kx[cnt].x=xx; Kx[cnt].y=yy; int tep= (xx-1) *9+yy; int a,b,c; Getplace (XX,YY,I,A,B,C); Dlx.link (Cnt,tep); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); tep= (xx-1) *9+yy+1; Getplace (XX,YY+1,J,A,B,C); Dlx.link (Cnt,tep); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); Dlx.link (cnt,81*4+ (i-1) *9+j); Dlx.link (cnt,81*4+ (j-1) *9+i); }} for (int xx=1; xx<=8; xx++) Vertical put {for (int yy=1; yy<=9; yy++) {cn t++; Kx[cnt].a=i; Kx[cnt].b=j; kx[cnt].f=1; Kx[cnt].x=xx; Kx[cnt].y=yy; int tep= (xx-1) *9+yy; int a,b,c; Getplace (XX,YY,I,A,B,C); Dlx.link (Cnt,tep); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); tep= (xx) *9+yy; Getplace (XX+1,YY,J,A,B,C); Dlx.link (Cnt,tep); Dlx.link (Cnt,a); Dlx.link (CNT,B); Dlx.link (CNT,C); Dlx.link (cnt,81*4+ (i-1) *9+j); Dlx.link (cnt,81*4+ (j-1) *9+i); } } }} dlx.dance (0); for (int i=0;i<dlx.cnt;i++) {//printf ("%d\n", Dlx.ans[i]); if (dlx.ans[i]>n+18) {int tep=dlx.ans[i]; MP[KX[TEP].X][KX[TEP].Y]=KX[TEP].A; if (kx[tep].f==0) mp[kx[tep].x][kx[tep].y+1]=kx[tep].b; else mp[kx[tep].x+1][kx[tep].y]=kx[tep].b; }} printf ("Puzzle%d\n", cas++); for (int i=1;i<=9;i++) {for (int j=1;j<=9;j++) printf ("%d", mp[i][j]); Puts (""); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[DLX precise coverage] HDU 4210 Su-domino-ku