题目:ZOJ - 3209
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.
舞蹈链讲解请看https://www.cnblogs.com/grenet/p/3145800.html;
题意:找出几张图拼出一张完整的地图;
分析:行p,列n*m,将每一张图分解成一个1*1 的正方形, 对大地图上的每一个小正方形进行编号,之后用给出的小地图进行填充,每一行就代表一个小地图。
但是要注意的是这道题需要求解出最小解,因此在DLX里边需要比较。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10005;
const int maxnode = 500005;
struct DLX
{
int n,m,sz;
int S[maxn],H[maxn];
int row[maxnode],col[maxnode];
int L[maxnode],R[maxnode],U[maxnode],D[maxnode];
int ansd,ans[maxnode];
void init(int _n,int _m)//_n是行数,_m是列数
{
n = _n;
m = _m;
for(int i = 0;i <= m;i ++)
{
S[i] = 0;
U[i] = i;D[i] = i;
L[i] = i - 1;
R[i] = i + 1;
}
R[m] = 0;
L[0] = m;
sz = m;
for(int i = 1;i <= n;i ++)
{
H[i] = -1;
}
}
void AddRow(int r,int c)//添加节点
{
++ S[col[++ sz] = c];
row[sz] = r;
D[sz] = D[c];
U[D[c]] = sz;
U[sz] = c;
D[c] = sz;
if(H[r] < 0) 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;
}
}
#define FOR(i,A,s) for(int i = A[s];i != s;i = A[i])
void removed(int c)//删除第c列
{
L[R[c]] = L[c];
R[L[c]] = R[c];
FOR(i,D,c)
FOR(j,R,i)
{
U[D[j]] = U[j];
D[U[j]] = D[j];
-- S[col[j]];
}
}
void restore(int c)//恢复第c列
{
FOR(i,U,c)
{
FOR(j,L,i)
{
++ S[col[U[D[j]] = D[U[j]] = j]];
}
}
L[R[c]] = c;
R[L[c]] = c;
}
void dfs(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(i,R,0)
if(S[i] < S[c]) c = i;
removed(c);
FOR(i,D,c)
{
ans[d] = row[i];
FOR(j,R,i) removed(col[j]);
dfs(d + 1);
FOR(j,L,i) restore(col[j]);
}
restore(c);
}
};
DLX slover;
int main()
{
int T;
int n,m,p;
cin >> T;
while(T --)
{
cin >> n >> m >>p;
slover.init(p,m * n);
int x1,x2,y1,y2;
for(int i = 1;i <= p;i ++)
{
cin >> x1 >> y1 >> x2 >> y2;
for(int j = y1;j < y2;j ++)
for(int k = x1 + 1;k <= x2;k ++)
{
int x = j * n + k;
slover.AddRow(i,x);
}
}
slover.ansd = -1;
slover.dfs(0);
printf("%d\n",slover.ansd);
}
return 0;
}