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;
}

ZOJ 3209 Treasure Map(精确覆盖)的更多相关文章

  1. ZOJ 3209 Treasure Map 精确覆盖

    题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...

  2. (简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。

    Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...

  3. zoj 3209.Treasure Map(DLX精确覆盖)

    直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...

  4. zoj - 3209 - Treasure Map(精确覆盖DLX)

    题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...

  5. ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )

    题意 :  给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m )  .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...

  6. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 KB Your boss once had got many copies of ...

  7. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...

  8. ZOJ 3209 Treasure Map DLX

    用最少的矩阵覆盖n*m的地图.注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位. #include <stdio ...

  9. ZOJ3209 Treasure Map —— Danc Links 精确覆盖

    题目链接:https://vjudge.net/problem/ZOJ-3209 Treasure Map Time Limit: 2 Seconds      Memory Limit: 32768 ...

随机推荐

  1. C++ 模板类友元之输出流操作符重载

    几个关键点: 需要前置声明!--奇怪的是别人告诉我也可以不需要,但我这里不行! 友元函数的函数名后面的<>,必须要有. #include <stdio.h> #include ...

  2. 学习 C++,关键是要理解概念,而不应过于深究语言的技术细节

    学习 C++学习 C++,关键是要理解概念,而不应过于深究语言的技术细节. 学习程序设计语言的目的是为了成为一个更好的程序员,也就是说,是为了能更有效率地设计和实现新系统,以及维护旧系统. C++ 支 ...

  3. js点击标签时获取当前标签属性值

    document.body.onclick=function(){ var obj = document.elementFromPoint(event.clientX,event.clientY); ...

  4. Oracle Net Manager 服务命名配置以及用PL/SQL 登陆数据库

    我们知道,要连接一个数据库需要知道四个参数: 1. 登陆用户名:user: 2. 登录密码:password: 3. 存放数据库的服务器地址(server_ip)和端口(server_port): 4 ...

  5. javascript 哈夫曼树构造

    function Node(data) { this.data = data; this.left = null; this.right = null; } Array.prototype.creat ...

  6. js时间转化

    const defaultTicks = 621355968000000000; export function convertDateToTicks(date = new Date()) { ret ...

  7. Ubuntu 13.10 安装 Oracle11gR2

    #step 1:  groupadd  -g 2000 dba useradd   -g 2000 -m -s /bin/bash -u 2000 grid useradd   -g 2000 -m ...

  8. 搭建 FTP 文件服务vsftpd

    安装并启动 FTP 服务 安装 VSFTPD 使用 yum 安装 vsftpd: yum install vsftpd -y vsftpd 是在 Linux 上被广泛使用的 FTP 服务器,根据其[官 ...

  9. mysql中,如何查看数据库中当前可用的校勘?字符集默认的collation?

    需求描述: mysql的字符集在使用的过程中会有一些规则,这些规则就组成了校勘, 也就是通过什么规则做什么事,比如,如何比较两个字符的大小,后台都是有一些 规则,这些规则就是校勘的一部分. 那么,查看 ...

  10. “浪潮杯”山东省第五届ACM大学生程序设计竞赛(总结贴)

    第一次參加省赛有点小激动,尽管是作为打星队參赛,但心情却是上下起伏. 5月9号晚上11点多到威海,有点略冷.可是空气比淄博好多了,大家到了旅馆的时候都非常晚了,抱怨了一下三星级的酒店的待遇,喝杯咖啡早 ...