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 <= 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

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了,按列向量压成一维却过了。。。不是很懂。。。

  1 //2017-04-15
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5
  6 using namespace std;
  7
  8 const int N = 510;
  9 const int M = 1005;
 10 const int maxnode = N*M;
 11 int p;
 12
 13 struct DLX
 14 {
 15     int n, m, sz;//n为矩阵行数,m为矩阵列数,sz为编号
 16     int U[maxnode], D[maxnode], R[maxnode], L[maxnode], Row[maxnode], Col[maxnode];//U、D、R、L分别记录上下右左域。Row[i]表示编号为i的节点所在的行号,Col[i]表示编号为i的节点所在的列号
 17     int H[N], S[M];//H[i]表示指向第i行最前边的节点,S[i]表示第i列1的个数
 18     int ansd, ans[N];
 19
 20     void init(int nn, int mm)
 21     {
 22         n = nn; m = mm;
 23         for(int i = 0; i <= m; i++)
 24         {
 25             S[i] = 0;//每一行1的个数初始化为0
 26             U[i] = D[i] = i;//最上面的一行表头C,上下域初始化都为自身
 27             L[i] = i-1;//左边
 28             R[i] = i+1;//右边
 29         }
 30         R[m] = 0; L[0] = m;//头尾特殊处理
 31         sz = m;
 32         for(int i = 1; i <= n; i++)H[i] = -1;
 33     }
 34     void link(int r, int c)//第r行第c列为1
 35     {
 36         ++S[Col[++sz] = c];//编号加1,记录列,所在的列1的个数加1
 37         Row[sz] = r;//记录行
 38         /*link上下域:*/
 39         D[sz] = D[c];
 40         U[D[c]] = sz;
 41         U[sz] = c;
 42         D[c] = sz;
 43         /*link左右域:*/
 44         if(H[r] < 0)H[r] = L[sz] = R[sz] = sz;
 45         else{
 46             R[sz] = R[H[r]];
 47             L[R[H[r]]] = sz;
 48             L[sz] = H[r];
 49             R[H[r]] = sz;
 50         }
 51     }
 52
 53     void Remove(int c)//删除第c列和其对应的行
 54     {
 55         L[R[c]] = L[c]; R[L[c]] = R[c];
 56         for(int i = D[c]; i != c; i = D[i])
 57               for(int j = R[i]; j != i; j = R[j])
 58             {
 59                 U[D[j]] = U[j];
 60                 D[U[j]] = D[j];
 61                 --S[Col[j]];
 62             }
 63     }
 64
 65     void resume(int c)//恢复第c列和其对应的行
 66     {
 67         for(int i = U[c]; i != c; i = U[i])
 68               for(int j = L[i]; j != i; j = L[j])
 69                   ++S[Col[U[D[j]]=D[U[j]]=j]];
 70         L[R[c]] = R[L[c]] = c;
 71     }
 72
 73     void Dance(int d)//d表示选了多少行
 74     {
 75         if(ansd != -1 && ansd <= d)return;//剪枝
 76         if(R[0] == 0)//0号节点为head节点
 77         {
 78             if(ansd == -1)ansd = d;
 79             else if(ansd > d)ansd = d;
 80             return;
 81         }
 82         int c = R[0];
 83         for(int i = R[0]; i != 0; i = R[i])//选出1最少的列
 84               if(S[i] < S[c])c = i;
 85         Remove(c);
 86         for(int i = D[c]; i != c; i = D[i])//枚举第c列存在1节点的行,进行递归处理
 87         {
 88             ans[d] = Row[i];//表示第d行选Row[i]
 89             for(int j = R[i]; j != i; j = R[j])Remove(Col[j]);//将这一行1节点所在的列都删除
 90             Dance(d+1);
 91             for(int j = L[i]; j != i; j = L[j])resume(Col[j]);//恢复
 92         }
 93         resume(c);
 94     }
 95 }dlx;
 96
 97 int main()
 98 {
 99     int n, m, T, x1, x2, y1, y2;
100     scanf("%d", &T);
101     while(T--)
102     {
103         scanf("%d%d%d", &n, &m, &p);
104         dlx.init(p, n*m);
105         for(int i = 1; i <= p; i++)
106         {
107             scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
108             for(int h = x1; h < x2; h++)
109                 for(int l = y1+1; l <= y2; l++)
110                     dlx.link(i, h*m+l);
111         }
112         dlx.ansd = -1;
113         dlx.Dance(0);
114         printf("%d\n", dlx.ansd);
115     }
116
117     return 0;
118 }
时间: 2024-11-03 03:27:18

ZOJ3209(KB3-B DLX)的相关文章

zoj3209(DLX)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=16234 题意:给p张小纸片, 问能不能选出尽量少的一部分或全部数量纸片来组成一个n*m大小的纸片, 要保证每两个纸片不能用重叠的部分 分析:把n*m的矩阵看成n*m个单位元,作为n*m列:每一个矩形分解成一行. 问题即转化为从这些行中选择最少的一部分使每一列被覆盖且仅覆盖一次. #include <cstdio> #include <cstring>

hdu 4735 Little Wish~ lyrical step~(DLX)

题目链接:hdu 4735 Little Wish~ lyrical step~ 题意: 有n个节点的树,每个节点可能是男孩,可能是女孩,节点之间有距离,现在要让所有的女孩周围距离D之内有男孩,问最小需要交换多少次男孩和女孩的位置. 题解: 把每个节点对小于D距离的全部link起来,然后DLX爆艹,意义就是选n个节点去覆盖全部节点,如果这个节点是女生,那么就是要替换的点.然后不断更新答案. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int

HDU 3335 Divisibility(DLX可重复覆盖)

Problem Description As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory.So,many people call him "the descendant of Chen Jingrun",which brings him a good reputation. AekdyCoin also plays an important role in th

hdu 2295 Radar(二分+DLX)

题目链接:hdu 2295 Radar 题意: 给你n个城市,m个雷达,现在最多用K个雷达,求最小半径覆盖全部的城市. 题解: 二分半径套一个DLX就行.网上随便找的一个板子 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 const double eps=1e-7; 5 6 struct Point{ 7 double x,y; 8 }city[100

DLx

1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cmath> 5 using namespace std; 6 const double eps=1e-8; 7 const int N=55,M=3005; 8 struct Point{int x,y;}c[N],r[N]; 9 double sqr(double x){return 1.0*x*x;} 1

搜索(DLX):HOJ 1017 - Exact cover

1017 - Exact cover Time Limit: 15s Memory Limit: 128MB Special Judge Submissions: 6751 Solved: 3519 Description There is an N*M matrix with only 0s and 1s, (1 <= N,M <= 1000). An exact cover is a selection of rows such that every column has a 1 in e

UVALive 2659+HUST 1017+ZOJ 3209 (DLX

UVALive 2659 题目:16*16的数独.试了一发大白模板. /* * @author: Cwind */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <map> #include <algorithm> #include <cstdio> #include <cstring> #include

UVa 1309 DLX Sudoku

16×16的数独. 看白书学的DLX,有些细节还有待消化,贴个模板先. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 8 const int maxn = 16; 9 const int maxnode = 16384 + 100; 10

[DLX重复覆盖] fzu 1686 神龙的难题

题意: 中文题 思路: 想到是一个重复覆盖的问题,然后就是最少放多少个能覆盖满. 建图的话就是先标记一下哪些点有怪物,最多就是n*m个怪物. 然后就是行. 行的话就看输入的x和y能框住多少的范围了. 然后四重循环遍历一遍建边就ok了. 代码: #include"stdio.h" #include"algorithm" #include"string.h" #include"iostream" #include"cma