(简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。
Description
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).
题目就是找到几个矩形来覆盖一个大的矩形,而且要求不能重叠。
构造01矩阵的话就是把n*m这个大矩形的每一个点都作为一列(也就是有n*m列),然后把每一个可选择矩形都当做一行。。。
代码如下:
#include<iostream>
#include<cstring> using namespace std; const int INF=10e8;
const int MaxN=;
const int MaxM=*;
const int MaxNode=*+; struct DLX
{
int n,m,size;
int U[MaxNode],D[MaxNode],L[MaxNode],R[MaxNode],col[MaxNode];
int S[MaxM],H[MaxN];
int ans; void init(int _n,int _m)
{
n=_n;
m=_m; ans=INF; for(int i=;i<=m;++i)
{
S[i]=;
U[i]=D[i]=i;
L[i]=i-;
R[i]=i+;
}
L[]=m;
R[m]=; size=m; for(int i=;i<=n;++i)
H[i]=-;
} void Link(int r,int c) //r is row , c is col.
{
col[++size]=c;
++S[c]; U[size]=U[c];
D[U[c]]=size;
D[size]=c;
U[c]=size; if(H[r]<)
H[r]=L[size]=R[size]=size;
else
{
R[size]=H[r];
L[size]=L[H[r]];
R[L[H[r]]]=size;
L[H[r]]=size;
}
} void remove(int c)
{
R[L[c]]=R[c];
L[R[c]]=L[c]; for(int i=D[c];i!=c;i=D[i])
for(int j=R[i];j!=i;j=R[j])
{
D[U[j]]=D[j];
U[D[j]]=U[j];
--S[col[j]];
}
} void resume(int c)
{
for(int i=U[c];i!=c;i=U[i])
for(int j=L[i];j!=i;j=L[j])
{
++S[col[j]];
D[U[j]]=U[D[j]]=j;
} R[L[c]]=L[R[c]]=c;
} void Dance(int d)
{
if(R[]==)
{
if(d<ans)
ans=d; return;
} if(d>ans)
return; int c=R[]; for(int i=R[];i!=;i=R[i])
if(S[i]<S[c])
c=i; remove(c); for(int i=D[c];i!=c;i=D[i])
{
for(int j=R[i];j!=i;j=R[j])
remove(col[j]); Dance(d+); for(int j=L[i];j!=i;j=L[j])
resume(col[j]);
} resume(c);
}
}; DLX dlx; int main()
{
ios::sync_with_stdio(false); int T;
int n,m,p;
int x1,x2,y1,y2;
cin>>T; while(T--)
{
cin>>n>>m>>p; dlx.init(p,(n)*(m)); for(int i=;i<=p;++i)
{
cin>>x1>>y1>>x2>>y2; for(int x=x1;x<x2;++x)
for(int y=y1;y<y2;++y)
dlx.Link(i,y*(n)+x+);
} dlx.Dance(); if(dlx.ans==INF)
cout<<-<<endl;
else
cout<<dlx.ans<<endl;
} return ;
}
(简单) ZOJ 3209 Treasure Map , DLX+精确覆盖。的更多相关文章
- zoj - 3209 - Treasure Map(精确覆盖DLX)
题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...
- ZOJ 3209 Treasure Map(精确覆盖)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- ZOJ 3209 Treasure Map DLX
用最少的矩阵覆盖n*m的地图.注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位. #include <stdio ...
- (简单) HUST 1017 Exact cover , DLX+精确覆盖。
Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is ...
- zoj 3209.Treasure Map(DLX精确覆盖)
直接精确覆盖 开始逐行添加超时了,换成了单点添加 #include <iostream> #include <cstring> #include <cstdio> ...
- ZOJ 3209 Treasure Map 精确覆盖
题目链接 精确覆盖的模板题, 把每一个格子当成一列就可以. S忘记初始化TLE N次, 哭晕在厕所...... #include<bits/stdc++.h> using namespac ...
- ZOJ 3209 Treasure Map (Dancing Links 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- ZOJ 3209 Treasure Map (Dancing Links)
Treasure Map Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit S ...
随机推荐
- html 页面视图中的资源文件(css/js/image)的路径问题。
说到html 页面视图中的资源文件的路径引用问题,这个问题以前一直没去弄明白.今天,我将公司新开发的一个项目完全移植到我本地搭建的php 环境中来,遇到了这个问题,想了一下,然后也不是很困难的就把它给 ...
- TCP数据包结构
源端口号( 16 位):它(连同源主机 IP 地址)标识源主机的一个应用进程.目的端口号( 16 位):它(连同目的主机 IP 地址)标识目的主机的一个应用进程.这两个值加上 IP 报头中的源主机 I ...
- UIImagePikerController 浅析
原文链接:http://www.jianshu.com/p/2ac85aca4468 UIImagePickerController是iOS系统提供的和系统的相册和相机交互的一个类,可以用来获取相册的 ...
- GUI矩形、椭圆、线、框架
所有的Swing组件必须由时间调度线程(event dispatch thread)进行配置,线程将鼠标点击和键盘敲击控制转移到用户接口组件.下面的代码片段是事件调度线程中的执行代码: EventQu ...
- HDU 4738 Caocao's Bridges(割边)
乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio& ...
- 转:loadruner报错:Step download timeout(120 seconds)的一个解决方法
一个网友问了我一个问题如下:loadruner报错:Error -27728: Step download timeout (120 seconds) 如何解决语法检查通过,但是在并发执行一个查询时候 ...
- android 5.0新特性学习--CardView
CardView继承自FrameLayout类,可以在一个卡片布局中一致性的显示内容,卡片可以包含圆角和阴影.CardView是一个Layout,可以布局其他View. 官网地址:https://de ...
- 【啊哈!算法】算法6:只有五行的Floyd最短路算法
暑假,小哼准备去一些城市旅游.有些城市之间有公路,有些城市之间则没有,如下图.为了节省经费以及方便计划旅程,小哼希望在出发之前知道任意两个城市之前的最短路程. 上图中有 ...
- 【使用Itext处理PDF文档(新建PDF文件、修改PDF文件、PDF中插入图片、将PDF文件转换为图片)】
iText简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- centos搭建nginx环境
1.yum install pcre* 2.wget http://nginx.org/download/nginx-1.7.8.tar.gz 3.tar -zxvf nginx-1.7.8.tar ...