ZOJ3209(KB3-B DLX)
Posted Penn000
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ3209(KB3-B DLX)相关的知识,希望对你有一定的参考价值。
Treasure Map
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了,按列向量压成一维却过了。。。不是很懂。。。
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 }
以上是关于ZOJ3209(KB3-B DLX)的主要内容,如果未能解决你的问题,请参考以下文章
ZOJ 3209 Treasure Map dancing links