Test instructions
Give you n pieces of debris that cannot be rotated or folded.
Ask if you can use some of these pieces to spell out 4*4 squares.
Ideas:
Cover the naked question exactly.
The map is to see where each fragment can be placed in the 4*4, as a row.
The column is 4*4=16, plus n fragments, which is 16+n.
and pay attention to the established decision.
Code:
#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include " Vector "#include" string "using namespace std; #define N 1005*1005#define RN 1005#define M 1005struct 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 | | R[0]>16) {cnt=x; return 1; } int now=r[0]; for (int i=r[0]; i!=0 && i<=16; 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{intM,id; int w[44];} Kx[1234];int Main () {int n; while (scanf ("%d", &n), n) {int cnt=0; Dlx.init (N*16,16+n); for (int i=1; i<=n; i++) {int A, B; scanf ("%d%d", &a,&b); Char v[12][12]; for (int j=0; j<a; j + +) scanf ("%s", V[j]); for (int xx=1, xx+a<=5; xx++) {for (int yy=1; yy+b<=5; yy++) { cnt++; kx[cnt].m=0; Kx[cnt].id=i; for (int x=0; x<a; x + +) {for (int y=0; y<b; y++) { if (v[x][y]== ' 1 ') {int tep= (xx+x-1) *4+ (Yy+y); Dlx.link (Cnt,tep); Kx[cnt].w[kx[cnt].m++]=tep; } } } Dlx.link (Cnt,16+i); }}} int f=dlx.dance (0); if (f==0) puts ("No solution possible"); else {char mp[10][10]; for (int i=0;i<dlx.cnt;i++) {for (int j=0;j<kx[dlx.ans[i]].m;j++) { int x, y; x= (kx[dlx.ans[i]].w[j]-1)/4; Y=kx[dlx.ans[i]].w[j]%4-1; if (y==-1) y=3; mp[x][y]=kx[dlx.ans[i]].id+ ' 0 '; }} for (int i=0;i<4;i++) {mp[i][4]= '); Puts (Mp[i]); }} puts (""); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[DLX precise coverage] HDU 1603 A puzzling problem