ZOJ3209-Treasure Map

19 篇文章 0 订阅

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.


Author:  HANG, Hang
Source:  The 6th Zhejiang Provincial Collegiate Programming Contest


题意:给你p个矩形,问能不能用这些矩形拼出一个n*m的矩形

解题思路:将矩形坐标转化为每个矩阵覆盖了哪些方格,然后舞蹈链精确覆盖


#include <iostream>    
#include <cstdio>    
#include <cstring>    
#include <string>    
#include <algorithm>    
#include <cctype>    
#include <map>    
#include <cmath>    
#include <set>    
#include <stack>    
#include <queue>    
#include <vector>    
#include <bitset>    
#include <functional>    

using namespace std;

#define LL long long    
const int INF = 0x3f3f3f3f;
const int maxn = 500005;

int n, m, K, x, y, X, Y;

struct DLX
{
	int L[maxn], R[maxn], U[maxn], D[maxn];
	int row[maxn], col[maxn], sum[maxn], ans[maxn];
	int n, m, num, cnt;
	int vis[maxn], flag[maxn];
	void add(int k, int l, int r, int u, int d, int x, int y)
	{
		L[k] = l;   R[k] = r;   U[k] = u;
		D[k] = d;   row[k] = x;  col[k] = y;
	}
	void reset(int n, int m)
	{
		num = 0x7FFFFFF;
		this->n = n;   this->m = m;
		for (int i = 0; i <= m; i++)
		{
			add(i, i - 1, i + 1, i, i, 0, i);
			sum[i] = 0;
		}
		L[0] = m, R[m] = 0, cnt = m + 1;
	}
	void insert(int x, int y)
	{
		int temp = cnt - 1;
		if (row[temp] != x)
		{
			add(cnt, cnt, cnt, U[y], y, x, y);
			U[D[cnt]] = cnt; D[U[cnt]] = cnt;
		}
		else
		{
			add(cnt, temp, R[temp], U[y], y, x, y);
			R[L[cnt]] = cnt; L[R[cnt]] = cnt;
			U[D[cnt]] = cnt; D[U[cnt]] = cnt;
		}
		sum[y]++, cnt++;
	}
	void remove(int k)
	{
		R[L[k]] = R[k], L[R[k]] = L[k];
		for (int i = D[k]; i != k; i = D[i])
			for (int j = R[i]; j != i; j = R[j])
			{
				D[U[j]] = D[j];
				U[D[j]] = U[j];
				sum[col[j]]--;
			}
	}
	void resume(int k)
	{
		R[L[k]] = k, L[R[k]] = k;
		for (int i = D[k]; i != k; i = D[i])
			for (int j = R[i]; j != i; j = R[j])
			{
				D[U[j]] = j;
				U[D[j]] = j;
				sum[col[j]]++;
			}
	}
	int A()
	{
		int dis = 0;
		for (int i = R[0]; i != 0; i = R[i]) vis[i] = 0;
		for (int i = R[0]; i != 0; i = R[i])
			if (!vis[i])
			{
				dis++, vis[i] = 1;
				for (int j = D[i]; j != i; j = D[j])
					for (int k = R[j]; k != j; k = R[k])
						vis[col[k]] = 1;
			}
		return dis;
	}
	void dfs(int k)
	{
		if (k + A() >= num) return;
		if (!R[0]) {num = min(k, num); return;}
		int now = R[0];
		for (int i = now; i != 0; i = R[i])
			if (sum[now]>sum[i]) now = i;
		remove(now);
		for (int i = D[now]; i != now; i = D[i])
		{
			for (int j = R[i]; j != i; j = R[j]) remove(col[j]);
			dfs(k + 1);
			for (int j = L[i]; j != i; j = L[j]) resume(col[j]);
		}
		resume(now);
	}
}dlx;

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &n, &m, &K);
		dlx.reset(K, n * m);
		for (int i = 1; i <= K; i++)
		{
			scanf("%d%d%d%d", &x, &y, &X, &Y);
			for (int j = x; j < X; j++)
				for (int k = y; k < Y; k++)
					dlx.insert(i, j * m + k + 1);
		}
		dlx.dfs(0);
		if (dlx.num <= K) printf("%d\n", dlx.num);
		else printf("-1\n");
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值