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.
题目链接:ZOJ-3209
题目大意:给p个方块,问最少的方块数目精确覆盖n*m的矩形。若不存在则输出-1。
题目思路:这是一个DLX精确覆盖的模板题。 //并不是很懂这个模板
把每个格子当成一个列,要覆盖所有格子。
取x行,使得每一列都存在一个1 —-> 从p中取x个格子。
以下是代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
//ps: 输入时,行列为1开始
const int maxnode = 500010;
const int MaxM = 1010;
const int MaxN = 510;
struct DLX
{
int n,m,size;
int U[maxnode],D[maxnode],R[maxnode],L[maxnode],Row[maxnode],Col[maxnode];
int H[MaxN],S[MaxM];
int ansd;
void init(int _n,int _m) //初始化
{
n = _n;
m = _m;
for(int i = 0;i <= m;i++)
{
S[i] = 0;
U[i] = D[i] = i; //建立双向十字链表
L[i] = i-1;
R[i] = i+1;
}
R[m] = 0; L[0] = m;
size = m;
for(int i = 1;i <= n;i++) H[i] = -1;
}
void Link(int r,int c) //输入1所在的行列坐标
{
++S[Col[++size]=c];
Row[size] = r;
D[size] = D[c];
U[D[c]] = size;
U[size] = c;
D[c] = size;
if(H[r] < 0)H[r] = L[size] = R[size] = size;
else
{
R[size] = R[H[r]];
L[R[H[r]]] = size;
L[size] = H[r];
R[H[r]] = size;
}
}
//这里的remove其实并没有真正删除掉结点,可以用resume恢复
void remove(int c) //删除第c列上的元素所在行
{
L[R[c]] = L[c]; R[L[c]] = R[c];
//删除c,c是列指针,删除了c就代表删除了整列,因为递归后不可能访问到c了
for(int i = D[c];i != c;i = D[i]) //c所在列上的元素i
for(int j = R[i];j != i;j = R[j]) //i和j一行的,删掉j
{
U[D[j]] = U[j];
D[U[j]] = D[j];
--S[Col[j]]; //j所在列的元素(‘1’的个数)-1;
}
}
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[U[D[j]]=D[U[j]]=j]];
L[R[c]] = R[L[c]] = c;
}
void Dance(int d)
{
//剪枝下
if(ansd != -1 && ansd <= d)return;
if(R[0] == 0)
{
if(ansd == -1)ansd = d;
else if(d < ansd)ansd = d;
return;
}
int c = R[0];
for(int i = R[0];i != 0;i = R[i]) if(S[i] < S[c]) c = i; //找元素最少的列c,一种优化
remove(c); //删除列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+1);
for(int j = L[i];j != i;j = L[j])resume(Col[j]);
}
resume(c);
}
};
DLX g;
//把每个格子当成一个列,要覆盖所有格子。
int main()
{
int t;
cin >> t;
while(t--)
{
int n,m,p;
cin >> n >> m >> p;
g.init(p,n * m); //初始化
for (int i = 1; i <= p; i ++)
{
int x1,x2,y1,y2;
cin >> x1 >> y1 >> x2 >> y2;
for (int j = x1 + 1; j<= x2; j++)
{
for (int k = y1 + 1; k <= y2; k++)
{
g.Link(i,k + (j - 1) * m); //记录为1的行列
}
}
}
g.ansd = -1; //记录答案
g.Dance(0);
cout << g.ansd << endl;
}
}