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 <= nm <= 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

精确覆盖问题:

1 0 0 1 0 0

0 1 0 1 0 1

0 1 1 0 0 0

0 0 0 0 1 0

在这样一个矩形中,找出最少的几行,使得这几行合起来,每一列都只有一个1,即这几行覆盖了每一列

解决精确覆盖问题的方法是Daning Links.网上有很多博客,

这道题目的意思是选择最少的矩形可以完全覆盖整个地图,不重复,

和精确覆盖如出一辙。可以转换一下,把n*m地图的每一个格子看成一个列

把小矩形看成一个行,小矩形里面的格子都是这个行里面包含的列。那么转换成精确覆盖的问题

然后套用模板。另外记得这道题目行是m,列是n.每个小矩形的格子

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
typedef long long int LL;
const int maxn=5e5; struct DACL
{
int H[maxn+5],S[maxn+5];
int u[maxn+5],d[maxn+5],l[maxn+5],r[maxn+5];
int Row[maxn+5],Col[maxn+5];
int n,m;
int size;
int ans;
void init(int x,int y)
{
n=x;
m=y;
for(int i=0;i<=m;i++)
{
u[i]=i;
d[i]=i;
l[i]=i-1;
r[i]=i+1;
S[i]=0;
}
l[0]=m;
r[m]=0;
size=m;
ans=-1;
for(int i=1;i<=n;i++)
H[i]=-1;
} void link(int row,int col)
{
Row[++size]=row;
Col[size]=col;
d[size]=d[col];
u[d[col]]=size;
u[size]=col;
d[col]=size;
if(H[row]==-1) H[row]=l[size]=r[size]=size;
else
{
l[size]=l[H[row]];
r[l[H[row]]]=size;
r[size]=H[row];
l[H[row]]=size;
}
S[col]++;
} void remove(int col)
{
l[r[col]]=l[col];
r[l[col]]=r[col];
for(int i=d[col];i!=col;i=d[i])
{
//cout<<i<<endl;
for(int j=r[i];j!=i;j=r[j])
{
// cout<<j<<endl;
u[d[j]]=u[j];
d[u[j]]=d[j];
S[Col[j]]--;
}
}
} void resurm(int col)
{
for(int i=u[col];i!=col;i=u[i])
{
//cout<<i<<endl;
for(int j=r[i];j!=i;j=r[j])
{
//cout<<j<<endl;
d[u[j]]=j;
u[d[j]]=j;
S[Col[j]]++;
}
}
l[r[col]]=col;
r[l[col]]=col;
} void dance(int dd)
{
if(ans!=-1&&dd>=ans) return;
if(r[0]==0)
{
if(ans==-1)
ans=dd;
else if(ans>dd)
ans=dd;
return;
}
int col=r[0];
for(int i=r[0];i!=0;i=r[i])
{
if(S[col]>S[i])
col=i;
}
remove(col);
for(int i=d[col];i!=col;i=d[i])
{
for(int j=r[i];j!=i;j=r[j])
{
//cout<<j<<endl;
remove(Col[j]); }
dance(dd+1);
for(int j=l[i];j!=i;j=l[j])
resurm(Col[j]); }
resurm(col); }
}temp;
int main()
{
int t;
scanf("%d",&t);
int p;
int x1,x2,y1,y2;
int n,m;
while(t--)
{
scanf("%d%d%d",&n,&m,&p);
temp.init(p,n*m);
for(int k=1;k<=p;k++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i=x1+1;i<=x2;i++)
{
for(int j=y1+1;j<=y2;j++)
{
temp.link(k,j+i*(m-1));
}
}
}
temp.dance(0);
printf("%d\n",temp.ans);
}
return 0;
}

最新文章

  1. Oracle11g CentOS7安装记录
  2. 三星嵌入式开发平台 三星Cortex-A9 4412 POP与SCP对比
  3. 使用已有PDB克隆PDB
  4. Java中的匿名类
  5. [leetcode]_Container With Most Water
  6. 《OD学hadoop》第三周0710
  7. poj 3279 Fliptile
  8. ORM之四:调用入口与调用示例
  9. HTML标签_Form
  10. 利用jquery实现百度新闻导航菜单滑动动画
  11. Java中对象的上转型对象
  12. 使用opencv传中文文件崩溃
  13. pinyin4j的使用
  14. 任务一:零基础HTML编码练习
  15. flex详解
  16. 要想成为前端大神,那些你不得不知晓的web前端命名规范。
  17. SharePoint 错误集
  18. oracle io 等待图解
  19. Django----ModelFrom
  20. JVM调优——之CMS GC日志分析

热门文章

  1. Hibernate查询_HQL_EJBQL_QBC_QBE
  2. Navicat连接Oracle11g 错误的解决办法
  3. vector 排序
  4. jQuery数组处理详解(转)
  5. 在VS中写js的同学注意了。。。。。。。。。。。。。。。。。。。
  6. 无法在Word中打开MathType怎么办
  7. 理解BSTR数据类型 神奇的BSTR
  8. php的优缺点
  9. linux中,查看某个命令是来自哪个RPM包或者是通过哪个RPM包安装的
  10. TTCN中PTC的执行流程