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
| Run ID | Submit Time | Judge Status | Problem ID | Language | Run Time(ms) | Run Memory(KB) | User Name |
| 2644280 | 2011-08-30 14:34:16 | Accepted | 3209 | C++ | 180 | 1156 | 魔神翼 |
#include<cstdio>
#define N 505
#define M 1005
int 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);
else
puts("-1");
}
}
本文深入探讨了信息技术领域的核心概念,包括前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具、嵌入式硬件、音视频基础、测试、基础运维、DevOps、操作系统、云计算厂商、自然语言处理、区块链、隐私计算、文档协作与知识管理等,全面阐述了各技术领域的特点、应用场景及最新发展趋势。
489

被折叠的 条评论
为什么被折叠?



