ZOJ3209Treasure Map精确覆盖DLX
来源:互联网 发布:电脑易教录屏软件 编辑:程序博客网 时间:2024/05/22 17:39
Your boss once had got many copies of a treasure map. Unfortunately, all the copies are now broken to many rectangular pieces, and what make it worse, he has lost some of the pieces. Luckily, it is possible to figure out the position of each piece in the original map. Now the boss asks you, the talent programmer, to make a complete treasure map with these pieces. You need to make only one complete map and it is not necessary to use all the pieces. But remember, pieces are not allowed to overlap with each other (See sample 2).
Input
The first line of the input contains an integer T (T <= 500), indicating the number of cases.
For each case, the first line contains three integers n m p (1 <= n, m <= 30, 1 <= p <= 500), the width and the height of the map, and the number of pieces. Thenp lines follow, each consists of four integersx1 y1 x2 y2 (0 <=x1 <x2 <=n, 0 <=y1 <y2 <= m), where (x1, y1) is the coordinate of the lower-left corner of the rectangular piece, and (x2, y2) is the coordinate of the upper-right corner in the original map.
Cases are separated by one blank line.
Output
If you can make a complete map with these pieces, output the least number of pieces you need to achieve this. If it is impossible to make one complete map, just output -1.Sample Input
3
5 5 1
0 0 5 5
5 5 2
0 0 3 5
2 0 5 5
30 30 5
0 0 30 10
0 10 30 20
0 20 30 30
0 0 15 30
15 0 30 30
Sample Output
1
-1
2
Hint
For sample 1, the only piece is a complete map.
For sample 2, the two pieces may overlap with each other, so you can not make a complete treasure map.
For sample 3, you can make a map by either use the first 3 pieces or the last 2 pieces, and the latter approach one needs less pieces.
Author: HANG, Hang
Source: The 6th Zhejiang Provincial Collegiate Programming Contest
#include<cstdio>#define N 505#define M 1005int m,n,H,cnt,size[M],ans;struct Node{ int r,c; Node *U,*D,*L,*R;}node[40005],row[N],col[M],head;void init(int r,int c){ cnt=0; head.r=r; head.c=c; head.L=head.R=head.U=head.D=&head; for(int i=0;i<c;i++){ col[i].r=r; col[i].c=i; col[i].L=&head; col[i].R=head.R; col[i].U=col[i].D=col[i].L->R=col[i].R->L=&col[i]; size[i]=0; } for(int i=r-1;i>=0;i--){ row[i].r=i; row[i].c=c; row[i].U=&head; row[i].D=head.D; row[i].L=row[i].R=row[i].U->D=row[i].D->U=&row[i]; }}void insert(int r,int c){ Node *p=&node[cnt++]; p->r=r; p->c=c; p->R=&row[r]; p->L=row[r].L; p->L->R=p->R->L=p; p->U=&col[c]; p->D=col[c].D; p->U->D=p->D->U=p; ++size[c];}void delLR(Node *p){ p->L->R=p->R; p->R->L=p->L;}void delUD(Node *p){ p->U->D=p->D; p->D->U=p->U;}void resumeLR(Node *p){p->L->R=p->R->L=p;}void resumeUD(Node *p){p->U->D=p->D->U=p;}void cover(int c){ if(c==H) return;Node *R,*C; delLR(&col[c]); for(C=col[c].D;C!=&col[c];C=C->D)for(R=C->L;R!=C;R=R->L){--size[R->c];delUD(R);}}void resume(int c){ if(c==H) return; Node *R,*C; for(C=col[c].U;C!=&col[c];C=C->U)for(R=C->R;R!=C;R=R->R){++size[R->c];resumeUD(R);} resumeLR(&col[c]);}void dfs(int k){ if(head.L==&head){if(k<ans)ans=k;return;}if(k>=ans)return; int INF=-1u>>1,c=-1;Node *p,*rc; for(p=head.L;p!=&head;p=p->L) if(size[p->c]<INF) INF=size[c=p->c];if(!INF)return; cover(c); for(p=col[c].D;p!=&col[c];p=p->D){ for(rc=p->L;rc!=p;rc=rc->L) cover(rc->c); dfs(k+1); for(rc=p->R;rc!=p;rc=rc->R) resume(rc->c); } resume(c);}int main(){int t,p;scanf("%d",&t); while(t--){int i,j,k,x0,x1,y0,y1;scanf("%d%d%d",&n,&m,&p);init(p+1,H=n*m);for(k=0;k<p;k++){scanf("%d%d%d%d",&x0,&y0,&x1,&y1);for(i=x0;i<x1;i++)for(j=y0;j<y1;j++)insert(k,i*m+j);}ans=999;dfs(0);if(ans<999)printf("%d\n",ans);elseputs("-1");}}
- ZOJ3209Treasure Map精确覆盖DLX
- DLX (精确覆盖) ZOJ 3209 Treasure Map
- ZOJ 3209 Treasure Map(DLX精确覆盖)
- ZOJ 3209 Treasure Map (DLX精确覆盖)
- DLX精确覆盖 zoj3209 Treasure Map
- ZOJ 3209 Treasure Map DLX精确覆盖
- ZOJ3209 Treasure Map(DLX精确覆盖)
- dlx 精确覆盖
- DLX精确覆盖poj1086
- zoj - 3209 - Treasure Map(精确覆盖DLX)
- ZOJ 3209 Treasure Map(DLX精确覆盖)
- 舞蹈链..Exact cover&Treasure Map等 . DLX-精确覆盖
- ZOJ 3209 Treasure Map (DLX精确覆盖)
- ZOJ 3209 Treasure Map (DLX精确覆盖问题)
- ZOJ 3290 Treasure Map(DLX+精确覆盖)
- DLX 精确覆盖 重复覆盖
- 【精确覆盖问题】DLX算法
- DLX精确覆盖 poj2676 Sudoku
- handle中传递对象
- 15个注意事项决定程序员的未来
- pymongo api
- Blackbox报告:如何防止创业公司死于拔苗助长
- hibernate中的 bag
- ZOJ3209Treasure Map精确覆盖DLX
- VIM 参考手册
- ns2中广播代理实现中的问题
- ANDROID取CPU序列号
- XSS偷取密码Cookies通用脚本
- c#字符串转unicode
- 开关和灯泡的对应关系
- fscanf用于分析文件中的两个重要用法
- android系统开发--HAL层开发基础