(简单) 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 ...
随机推荐
- How To Add Swap on Ubuntu 14.04
https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 How To Add Swap on ...
- iOS 开发之照片框架详解
转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...
- word2010无法打开文件时的一点对策
word2010无法打开文件时的一点对策 1. Word 安全模式启动,点击「开始」,在搜索栏中输入winword /safe并回车,测试问题是否依然存在? 2. 正常启动Word,点击“文件”—“选 ...
- AutoTile 自动拼接(六 大结局) 学习与实践
昨天在网上找了一些资源图片,这回就不用担心 背景资源不多的问题了,现在我一边 制作,一边发布文章. 各种各样,500多个,这里还是特别感谢 ,万恶的资本主义,不设密码就给我分享. 在制作前,大家看下这 ...
- java中list的使用方法
LIST是个容器接口,可以理解为动态数组,传统数组必须定义好数组的个数才可以使用,而容器对象无须定义好数组下标总数, 用add()方法即可添加新的成员对象,他可以添加的仅仅只能为对象,不能添加基本数据 ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
- w3chtml页面和css书写规范
http://www.cnblogs.com/Wenwang/archive/2011/09/07/2169881.html
- Html基础详解之(jquery)
jquery选择器: #id 根据给定的ID匹配一个元素,如果选择器中包含特殊字符,可以用两个斜杠转义.(注:查找 ID 为"myDiv"的元素.) <!DOCTYPE ht ...
- a java runtime environment(JRE) or java development kit(JDK) must be....
错误: 解决方法: 系统变量里设置下面: 变量名:JAVA_HOME 变量值:C:\Program Files\Java\jdk1.6.0_25 变量名:CLASSPATH 变量值:.;%JAVA_H ...
- 常用Select语句
--语 句 功 能--数据操作SELECT --从数据库表中检索数据行和列INSERT --向数据库表添加新数据行DE ...