[DLX精确覆盖] hdu 1603 A Puzzling Problem
来源:互联网 发布:菊水 清酒 知乎 编辑:程序博客网 时间:2024/06/05 00:42
题意:
给你n块碎片,这些碎片不能旋转、翻折。
问你能不能用其中的某些块拼出4*4的正方形。
思路:
精确覆盖裸题了
建图就是看看每个碎片在4*4中能放哪些位置,这个就作为行。
列就是4*4=16个位置再加上n个碎片也就是16+n
然后注意下成立的判定就好了
代码:
#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{ int m,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]='\0'; puts(mp[i]); } } puts(""); } return 0;}
0 0
- [DLX精确覆盖] hdu 1603 A Puzzling Problem
- HDU 4069 Squiggly Sudoku DLX 精确覆盖
- [DLX精确覆盖] hdu 3663 Power Stations
- HDU 3663 Power Stations DLX精确覆盖
- UVa387 - A Puzzling Problem
- uva387 A Puzzling Problem
- dlx 精确覆盖
- DLX精确覆盖poj1086
- DLX 精确覆盖 重复覆盖
- hdu 4979 A simple math problem. DLX(多重覆盖)+打表
- hdu - 4979 - A simple math problem.(可重复覆盖DLX + 打表)
- DLX(精确覆盖) HDU 3663 Power Stations
- [DLX精确覆盖] hdu 4210 Su-domino-ku
- [DLX精确覆盖+打表] hdu 2518 Dominoes
- HDU 3909 Sudoku(数独转DLX精确覆盖)
- HDU 3498 whosyourdaddy 重复覆盖 DLX+A*
- uva 387 - A Puzzling Problem
- uva 387 A Puzzling Problem
- OC类之 Dictionary
- 自定义Dialog
- ADB端口被占用,adb server is out of date
- Flying to the Mars
- Linux下新建分区步骤
- [DLX精确覆盖] hdu 1603 A Puzzling Problem
- python scrapy 爬虫 未完待续
- The resource identified by this request is only capable of generating responses with characteristics
- 收集常用的.net开源项目
- leetCode 83.Remove Duplicates from Sorted List(删除排序链表的重复) 解题思路和方法
- 提升linux下tcp服务器并发连接数限制
- java中的开方Math.sqrt(n)函数和平方{a的b次方Math.pow(a, b)}
- 图片折叠效果:Layer的contentsRect属性和渐变层
- EXITTHRD.C--示范ExitThread()