ZOJ3209(KB3-B DLX)
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 <= 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 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
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.
精确覆盖问题,使用DLX,将图按行向量压成一维就TLE了,按列向量压成一维却过了。。。不是很懂。。。
- //2017-04-15
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int N = ;
- const int M = ;
- const int maxnode = N*M;
- int p;
- struct DLX
- {
- int n, m, sz;//n为矩阵行数,m为矩阵列数,sz为编号
- int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];//U、D、R、L分别记录上下右左域。Row[i]表示编号为i的节点所在的行号,Col[i]表示编号为i的节点所在的列号
- int H[N], S[M];//H[i]表示指向第i行最前边的节点,S[i]表示第i列1的个数
- int ansd, ans[N];
- void init(int nn, int mm)
- {
- n = nn; m = mm;
- for(int i = ; i <= m; i++)
- {
- S[i] = ;//每一行1的个数初始化为0
- U[i] = D[i] = i;//最上面的一行表头C,上下域初始化都为自身
- L[i] = i-;//左边
- R[i] = i+;//右边
- }
- R[m] = ; L[] = m;//头尾特殊处理
- sz = m;
- for(int i = ; i <= n; i++)H[i] = -;
- }
- void link(int r, int c)//第r行第c列为1
- {
- ++S[Col[++sz] = c];//编号加1,记录列,所在的列1的个数加1
- Row[sz] = r;//记录行
- /*link上下域:*/
- D[sz] = D[c];
- U[D[c]] = sz;
- U[sz] = c;
- D[c] = sz;
- /*link左右域:*/
- if(H[r] < )H[r] = L[sz] = R[sz] = sz;
- else{
- R[sz] = R[H[r]];
- L[R[H[r]]] = sz;
- L[sz] = H[r];
- R[H[r]] = sz;
- }
- }
- void Remove(int c)//删除第c列和其对应的行
- {
- L[R[c]] = L[c]; R[L[c]] = R[c];
- for(int i = D[c]; i != c; i = D[i])
- for(int j = R[i]; j != i; j = R[j])
- {
- U[D[j]] = U[j];
- D[U[j]] = D[j];
- --S[Col[j]];
- }
- }
- void resume(int c)//恢复第c列和其对应的行
- {
- for(int i = U[c]; i != c; i = U[i])
- for(int j = L[i]; j != i; j = L[j])
- ++S[Col[U[D[j]]=D[U[j]]=j]];
- L[R[c]] = R[L[c]] = c;
- }
- void Dance(int d)//d表示选了多少行
- {
- if(ansd != - && ansd <= d)return;//剪枝
- if(R[] == )//0号节点为head节点
- {
- if(ansd == -)ansd = d;
- else if(ansd > d)ansd = d;
- return;
- }
- int c = R[];
- for(int i = R[]; i != ; i = R[i])//选出1最少的列
- if(S[i] < S[c])c = i;
- Remove(c);
- for(int i = D[c]; i != c; i = D[i])//枚举第c列存在1节点的行,进行递归处理
- {
- ans[d] = Row[i];//表示第d行选Row[i]
- for(int j = R[i]; j != i; j = R[j])Remove(Col[j]);//将这一行1节点所在的列都删除
- Dance(d+);
- for(int j = L[i]; j != i; j = L[j])resume(Col[j]);//恢复
- }
- resume(c);
- }
- }dlx;
- int main()
- {
- int n, m, T, x1, x2, y1, y2;
- scanf("%d", &T);
- while(T--)
- {
- scanf("%d%d%d", &n, &m, &p);
- dlx.init(p, n*m);
- for(int i = ; i <= p; i++)
- {
- scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
- for(int h = x1; h < x2; h++)
- for(int l = y1+; l <= y2; l++)
- dlx.link(i, h*m+l);
- }
- dlx.ansd = -;
- dlx.Dance();
- printf("%d\n", dlx.ansd);
- }
- return ;
- }
ZOJ3209(KB3-B DLX)的更多相关文章
- zoj3209(DLX)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16234 题意:给p张小纸片, 问能不能选出尽量少的一部分或全部数量 ...
- DLX 舞蹈链 精确覆盖 与 重复覆盖
精确覆盖问题:给定一个由0-1组成的矩阵,是否能找到一个行的集合,使得集合中每一列都恰好包含一个1 还有重复覆盖问题 dancing links 是 一种数据结构,用来优化搜索,不算是一种算法.(双向 ...
- ZOJ3209 Treasure Map —— Danc Links 精确覆盖
题目链接:https://vjudge.net/problem/ZOJ-3209 Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 ...
- DLX (poj 3074)
题目:Sudoku 匪夷所思的方法,匪夷所思的速度!!! https://github.com/ttlast/ACM/blob/master/Dancing%20Link%20DLX/poj%2030 ...
- HDU 3957 Street Fighter(搜索、DLX、重复覆盖+精确覆盖)
很久以前就看到的一个经典题,一直没做,今天拿来练手.街霸 给n<=25个角色,每个角色有 1 or 2 个版本(可以理解为普通版以及爆发版),每个角色版本可以KO掉若干人. 问最少选多少个角色( ...
- 数独求解 DFS && DLX
题目:Sudoku 题意:求解数独.从样例和结果来看应该是简单难度的数独 思路:DFS 设置3个数组,row[i][j] 判断第i行是否放了j数字,col[i][j] 判断第i列是否放了j数字.squ ...
- DLX模型问题
问题:sevenzero liked Warcraft very much, but he haven't practiced it for several years after being add ...
- HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...
- HDU 5046 Airport(dlx)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5046 题意:n个城市修建m个机场,使得每个城市到最近进场的最大值最小. 思路:二分+dlx搜索判定. ...
随机推荐
- 动态分析小示例| 08CMS SQL 注入分析
i春秋作家:yanzm 0×00 背景 本周,拿到一个源码素材是08cms的,这个源码在官网中没有开源下载,需要进行购买,由某师傅提供的,审计的时候发现这个CMS数据传递比较复杂,使用静态分析的方式不 ...
- Java实时监控类库Metrics
随着系统越来越大,越来越复杂,我们需要在业务方面加上一些监控服务.Metrics作为一款监控指标的度量类库,提供了许多工具帮助开发者来完成自定义的监控工作. 使用Metrics 使用Metrics,只 ...
- centos 安装oracle 11g r2(三)-----表空间创建
centos 安装oracle 11g r2(三)-----表空间创建 创建表空间前要保证监听与数据库实例已经启动 1.启动监听 [oracle@localhost ~]$ lsnrctl start ...
- 组件基础(插槽slot)—Vue学习笔记
刚开始我们淡淡提过<slot></slot>现在深入了解一下. slot可以进行父组件传值到子组件. 比如:我们将hiboy通过<slot>传递到组件中. < ...
- sync.Pool 资源池
sync.Pool type Pool struct { // 可选参数New指定一个函数在Get方法可能返回nil时来生成一个值 // 该参数不能在调用Get方法时被修改 New func() in ...
- SQLAlchemy介绍和基本使用
pymysql:pymysql是用Python来操作mysql的包,因此通过pip来安装,命令如下:pip3 install pymysql.如果您用的是Python 3,请安装pymysql 豆瓣源 ...
- git中多账号切换问题的解决方案(转)
作者:知乎用户链接:https://www.zhihu.com/question/23028445/answer/416231632来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- Eclipse个人规范化设置
为保证在各个在各个系统中获得的代码样式保持一致,规范法化开发,对Eclipse进行一些常用通用设置: 1. 代码块缩进 4个空格,如果使用 tab缩进,请设置成 1个 tab为 4个空格.(阿里巴巴开 ...
- 查漏补缺之开g的正则
当正则表达式开了挂,就会多一个g的修饰符,用于表示全局匹配.然而这个表达式却不仅仅是多了个g这么简单,它的方法也会发生改变.由于之前不是太了解,今天好好捋一下,且听我细细道来. 正则表达式的方法和属性 ...
- php -- 数学函数
----- 016-math.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv="c ...