ZOJ 3209 Treasure Map (Dancing Links)
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
System Crawler (2015-04-09)
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).
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 nmp (1 <= n, m <= 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 x1y1x2y2 (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.
草泥马啊!!!!N和M弄反了啊!!!!!T了一下午啊!!!!!!!!找BUG都快找哭了啊!!!!!!!!!!
丧心病狂弄了个M行N列啊!!!!!!!!!!这尼玛反人类啊!!!!!!活生生由300ms优化到了80ms啊!!!!!!!!!
#include <iostream>
#include <cstdio>
using namespace std; const int SIZE = * ;
const int HEAD = ;
int U[SIZE],D[SIZE],L[SIZE],R[SIZE],C[SIZE],S[SIZE];
int N,M;
int ANS = 0x7fffffff; void ini(void);
void dancing(int ans);
void remove(int);
void resume(int);
int main(void)
{
int t,p,count;
int x_1,y_1,x_2,y_2;
int col; scanf("%d",&t);
while(t --)
{
ANS = 0x7fffffff;
scanf("%d%d%d",&N,&M,&p);
ini();
count = N * M + ;
while(p --)
{
scanf("%d%d%d%d",&x_1,&y_1,&x_2,&y_2); int first = count;
for(int i = x_1;i < x_2;i ++)
for(int j = y_1;j < y_2;j ++)
{
col = i * M + j + ;
U[count] = U[col];
D[count] = col;
L[count] = count - ;
R[count] = count + ; D[U[col]] = count;
U[col] = count; C[count] = col;
++ S[col];
++ count;
}
R[count - ] = first;
L[first] = count - ;
}
dancing();
if(ANS == 0x7fffffff)
puts("-1");
else
printf("%d\n",ANS);
} return ;
} void ini(void)
{
L[HEAD] = N * M;
R[HEAD] = ;
U[HEAD] = D[HEAD] = S[HEAD] = C[HEAD] = HEAD; for(int i = ;i <= N * M;i ++)
{
U[i] = D[i] = i;
L[i] = i - ;
R[i] = i + ; C[i] = i;
S[i] = ;
}
R[N * M] = ;
} void dancing(int ans)
{
if(ans >= ANS)
return ; if(R[HEAD] == HEAD)
{
ANS = ans < ANS ? ans : ANS;
return ;
} int min_loc = R[HEAD];
for(int i = R[HEAD];i;i = R[i])
if(S[i] < S[min_loc])
min_loc = i; remove(min_loc);
for(int i = D[min_loc];i != min_loc;i = D[i])
{
for(int j = R[i];j != i;j = R[j])
remove(C[j]);
dancing(ans + );
for(int j = L[i];j != i;j = L[j])
resume(C[j]);
}
resume(min_loc); } 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[C[j]];
}
} void resume(int c)
{
R[L[c]] = c;
L[R[c]] = c; for(int i = U[c];i != c;i = U[i])
for(int j = R[i];j != i;j = R[j])
{
D[U[j]] = j;
U[D[j]] = j;
++ S[C[j]];
}
}
ZOJ 3209 Treasure Map (Dancing Links)的更多相关文章
- 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 精确覆盖 )
题意 : 给你一个大小为 n * m 的矩形 , 坐标是( 0 , 0 ) ~ ( n , m ) .然后给你 p 个小矩形 . 坐标是( x1 , y1 ) ~ ( x2 , y2 ) , 你选 ...
- 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+精确覆盖。
Description Your boss once had got many copies of a treasure map. Unfortunately, all the copies are ...
- ZOJ 3209 Treasure Map DLX
用最少的矩阵覆盖n*m的地图.注意矩阵不能互相覆盖. 这里显然是一个精确覆盖,但因为矩阵拼接过程中,有公共的边,这里须要的技巧就是把矩阵的左边和以下截去一个单位. #include <stdio ...
- 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(精确覆盖DLX)
题意:一个 n x m 的矩形(1 <= n, m <= 30),现给出这个矩形中 p 个(1 <= p <= 500)子矩形的左下角与右下角坐标,问最少用多少个子矩形能够恰好 ...
- ZOJ3209 Treasure Map —— Danc Links 精确覆盖
题目链接:https://vjudge.net/problem/ZOJ-3209 Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 ...
随机推荐
- Nginx和Tengine的详细安装图文教程(Linux下)
简洁安装 安装依赖 yum -y install gcc openssl-devel pcre-devel zlib-devel 编译三步走./configure \ --prefix=/opt/sx ...
- Hibernate之QBC查询与本地SQL查询
1. QBC查询: QBC 查询就是通过使用Hibernate提供的QueryByCriteria API 来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口 ...
- python 操作 excel
python操作execel主要是读写 读 通过 http://pypi.python.org/pypi/xlrd 写 通过 http://pypi.python.org/pypi/xlwd 下载ta ...
- DB2 递归查询
上一篇中讲解了ORACLE中的递归查询,下面我们看一下DB2中如何使用递归查询: 同样的我们先新建一个表来存储以上信息,并插入测试数据: --建表 create table FAMILY ( pers ...
- HDU 5842 Lweb and String (水题)
Lweb and String 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5842 Description Lweb has a string S ...
- 关于DateTime.Now.Ticks
DataTime.Now.Ticks 的值表示自 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔的间隔数,可用于较精确的计时. 1秒=1000豪秒 1毫秒=10 ...
- linux中crontab实现以秒执行任务
用crontab+sleep实现以秒执行任务 crontab -e * * * * * /bin/date >>/tmp/date.txt * * * * * sleep 10s; /bi ...
- Android应用之《宋词三百首》(二)
接上回,上回我们讲到MainActivity里面将所有的宋词标题和作者显示到界面的ListView中去,我们接下来的工作是通过点击ListView的Item跳转到ContentActivity里面去显 ...
- codeforces Gym 100418D BOPC 打表找规律,求逆元
BOPCTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action?c ...
- android4.0蓝牙使能的详细解析
本文详细分析了android4.0 中蓝牙使能的过程,相比较android2.3,4.0中的蓝牙最大的差别在于UI上on/off的伪开关.在android4.0中加入了 adapter的状态机.所谓的 ...