题目来源:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3372

Treasure Map

Time Limit: 2 Seconds      Memory Limit: 32768 KB

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
题目大意:给p张小纸片, 问能不能选其中的一部分或者全部来组成一个n*m大小的纸片, 要保证每两个纸片不能用重叠的部分。
思路:n*m的矩形纸片分成n*m个小单位,然后所有的单位构成一行,对于每一列所有能覆盖该单位的纸片都在这一列, 即建成一个0-1矩阵,之后用
精确覆盖就可以了!!
 61     for(Min=ROW,i=R[0];i;i=R[i])
62
if(Min>S[i]) Min=S[i],c=i;
每次选取元素最少的列, 这个地方改成Min>=S[i]就超时了,很纳闷了。。。
Run ID Submit Time Judge Status Problem ID Language Run Time(ms) Run Memory(KB) User Name
2618525 2011-08-07 17:13:52 Accepted 3209 C++ 860 9012 zyzamp
2618298 2011-08-07 13:06:54 Accepted 3209 C++ 860 9012 zyzamp
code:
View Code
  1 # include<stdio.h>
2 # include<string.h>
3 # define CL 905
4 # define ROW 505
5 # define V 452000
6 int R[V],L[V];
7 int U[V],D[V];
8 int C[V];
9 int H[ROW],S[CL];
10 int ak,n,m,size,p;
11 void Link(int r,int c)
12 {
13 S[c]++;C[size]=c;
14 U[size]=U[c];D[U[c]]=size;
15 D[size]=c;U[c]=size;
16 if(H[r]==-1) H[r]=L[size]=R[size]=size;
17 else
18 {
19 L[size]=L[H[r]];R[L[H[r]]]=size;
20 R[size]=H[r];L[H[r]]=size;
21 }
22 size++;
23 }
24 void remove(int c)
25 {
26 int i,j;
27 L[R[c]]=L[c];
28 R[L[c]]=R[c];
29 for(i=D[c];i!=c;i=D[i])
30 {
31 for(j=R[i];j!=i;j=R[j])
32 {
33 S[C[j]]--;
34 U[D[j]]=U[j];
35 D[U[j]]=D[j];
36 }
37 }
38 }
39 void resume(int c)
40 {
41 int i,j;
42 for(i=U[c];i!=c;i=U[i])
43 {
44 for(j=L[i];j!=i;j=L[j])
45 {
46 S[C[j]]++;
47 U[D[j]]=D[U[j]]=j;
48 }
49 }
50 L[R[c]]=c;
51 R[L[c]]=c;
52 }
53 void Dance(int k)
54 {
55 int i,j,Min,c;
56 if(!R[0])
57 {
58 if(k<ak) ak=k;
59 return;
60 }
61 for(Min=ROW,i=R[0];i;i=R[i])
62 if(Min>S[i]) Min=S[i],c=i;
63 remove(c);
64 for(i=D[c];i!=c;i=D[i])
65 {
66 for(j=R[i];j!=i;j=R[j])
67 remove(C[j]);
68 Dance(k+1);
69 for(j=L[i];j!=i;j=L[j])
70 resume(C[j]);
71 }
72 resume(c);
73 }
74 int main()
75 {
76 int i,j,ncase,x1,y1,x2,y2,k;
77 scanf("%d",&ncase);
78 while(ncase--)
79 {
80 scanf("%d%d%d",&n,&m,&p);
81 for(i=0;i<=m*n;i++)
82 {
83 S[i]=0;
84 U[i]=D[i]=i;
85 L[i+1]=i;R[i]=i+1;
86 }
87 R[n*m]=0;
88 size=n*m+1;
89 memset(H,-1,sizeof(H));
90 for(i=1;i<=p;i++)
91 {
92 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
93 for(j=y1;j<=y2-1;j++)
94 for(k=x1+1;k<=x2;k++)
95 Link(i,j*n+k);
96 }
97 ak=ROW;
98 Dance(0);
99 printf("%d\n",ak==ROW?-1:ak);
100 }
101 return 0;
102 }
posted on 2011-08-07 17:25  奋斗青春  阅读(493)  评论(0编辑  收藏  举报