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.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=treasure_map.jpg)
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来暴搜,行表示的是选择这个地图,列的话表示这个地图碎片包含的原地图的哪些格子。由于每个格子只能被覆盖一遍,所以这是一个精确覆盖问题。精确覆盖的意思是在一个矩形中选取一些行,使得这些行组合起来后,每一列都有且仅有一个1. 那么为什么要用Dancing Link呢? 其实他就是一个十字链表,他最大的特点就是能够在删除一个节点后快速的恢复节点。 当我们选择了某一行,我们把对应的列全部删去,也把对应列有1的行全部删去,这样我们就能减少不必要的遍历了。所以DLX 能够做出常数的优化。
代码:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
const int maxn = 30 * 30 * 500 + 5;
int n, m, sz;
int U[maxn], D[maxn], L[maxn], R[maxn];
int S[30 * 30 + 5], cnt;
int row[maxn], col[maxn];
inline int getx(int r, int c) { return r*m + c+1; }
#define FOR(i,s,A) for(int i=A[s];i!=s;i=A[i])
void add_row(int r,int * ocp)
{
int q = cnt;
for (int i = 1; i <= n*m; ++i) if (ocp[i])
{
U[cnt] = U[i]; D[cnt] = i;
L[cnt] = cnt - 1, R[cnt] = cnt + 1;
D[U[cnt]] = cnt; U[i] = cnt;
row[cnt] = r, col[cnt] = i;
++S[i];
++cnt;
}
L[q] = cnt - 1; R[cnt - 1] = q;
}
void RemoveColumn(int c)
{
L[R[c]] = L[c]; R[L[c]] = R[c];
FOR(i, c, D) {
FOR(j, i, R) {
--S[col[j]];
U[D[j]] = U[j], D[U[j]] = D[j];
}
}
}
void ResumeColumn(int c)
{
FOR(i, c, U) {
FOR(j, i, L) {
++S[col[j]];
U[D[j]] = D[U[j]] = j;
}
}
L[R[c]] = R[L[c]] = c;
}
int ocp[30*30+5];
void input()
{
scanf("%d%d%d", &n, &m, &sz);
cnt = 0;
for (int i = 0; i <= n*m; ++i) {
L[cnt] = cnt - 1, R[cnt] = cnt + 1;
U[cnt] = D[cnt] = cnt;
col[cnt] = i;
++cnt;
}
memset(S, 0, sizeof(S));
L[0] = cnt - 1, R[cnt - 1] = 0;
for (int i = 0; i < sz; ++i) {
int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
memset(ocp, 0, sizeof(ocp));
for (int r = x1; r < x2;++r)
for (int c = y1; c < y2; ++c)
ocp[getx(r, c)] = 1;
add_row(i + 1, ocp);
}
}
int ans;
void dfs(int step)
{
if (ans <= step) return;
if (R[0] == 0) {
ans = step;
return;
}
int c = R[0];
for (int i = R[0]; i != 0; i = R[i])
if (S[c] > S[i]) c = i;
char a = 'a';
if (S[c] == 0) return;
RemoveColumn(c);
// printf("(%d,%d)\n", (c - 1) / m, (c - 1) % m);
for (int i = D[c]; i != c; i = D[i]) {
// printf("%d\n", row[i]);
// printf("(%d,%d)\n", (col[i] - 1) / m, (col[i] - 1) % m);
for (int j = R[i]; j != i; j = R[j]) RemoveColumn(col[j]);
dfs(step + 1);
for (int j = L[i]; j != i; j = L[j]) ResumeColumn(col[j]);
}
ResumeColumn(c);
}
void solve()
{
ans = sz + 1;
dfs(0);
if (ans == sz + 1) printf("-1\n");
else printf("%d\n", ans);
}
int main()
{
int T; cin >> T;
while (T--) {
input();
solve();
}
}