您的位置:首页 > 其它

zoj 3209 Treasure Map 精确覆盖 DLX 给一些指定位置的矩形,使用最少的矩形去精确覆盖一个大的矩形,矩形间不能重叠。

2011-09-15 21:37 447 查看
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. Then p lines follow, each consists of four integers x1 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.

//

这题的关键就是把它转换为01模型。

我们把矩形块作为行,把所有的格子作为列。

这样,就有500行 ,有30*30列。

然后,很简单地做Dancing Links就可以了。

#include<stdio.h>

#include<string.h>

#include<time.h>

#include<limits.h>

#define N 1005

#define V 1020005

int U[V],D[V];

int L[V],R[V];

int C[V];

int H
,S
,mark[V];

int size,n,m,OK
,flag;

void Link(int r,int c)

{

S[c]++;C[size]=c;

U[size]=U[c];D[U[c]]=size;

D[size]=c;U[c]=size;

if(H[r]==-1) H[r]=L[size]=R[size]=size;

else

{

L[size]=L[H[r]];R[L[H[r]]]=size;

R[size]=H[r];L[H[r]]=size;

}

mark[size]=r;

size++;

}

void remove(int c)//删除列

{

int i,j;

L[R[c]]=L[c];

R[L[c]]=R[c];

for(i=D[c];i!=c;i=D[i])

{

for(j=R[i];j!=i;j=R[j])

{

U[D[j]]=U[j],D[U[j]]=D[j];

S[C[j]]--;

}

}

}

void resume(int c)

{

int i,j;

for(i=U[c];i!=c;i=U[i])

{

for(j=L[i];j!=i;j=L[j])

{

U[D[j]]=j;D[U[j]]=j;

S[C[j]]++;

}

}

L[R[c]]=c;

R[L[c]]=c;

}

int ans;//最小解

void Dance(int k)

{

if(k>=ans) return ;

int i,j,Min,c;

if(!R[0])

{

ans=k;

flag=1;

// printf("%d",k);

// for(i=0;i<k;i++)

// printf(" %d",mark[OK[i]]);

// printf("\n");

return;

}

for(Min=N,i=R[0];i;i=R[i])

if(S[i]<Min) Min=S[i],c=i;

remove(c);//删除该列

for(i=D[c];i!=c;i=D[i])

{

OK[k]=i;

for(j=R[i];j!=i;j=R[j])

remove(C[j]);

Dance(k+1);

//if(flag) return;

for(j=L[i];j!=i;j=L[j])

resume(C[j]);

}

resume(c);

}

int main()

{

int i,j,num;

int tn,tm,tp;

int ci;scanf("%d",&ci);

while(ci--)

{

scanf("%d%d%d",&tn,&tm,&tp);

n=tp;m=tn*tm;

for(i=0;i<=m;i++)

{

S[i]=0;

D[i]=U[i]=i;

L[i+1]=i;R[i]=i+1;

}R[m]=0;

size=m+1;

memset(H,-1,sizeof(H));

memset(mark,0,sizeof(mark));

for(i=1;i<=n;i++)

{

int x1,y1,x2,y2;scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

x1++,y1++;

for(int j=x1;j<=x2;j++)

for(int k=y1;k<=y2;k++)

{

Link(i,(j-1)*tm+k);

}

}

flag=0;

ans=INT_MAX;

Dance(0);

if(!flag) printf("-1\n");

else printf("%d\n",ans);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: