ZOJ - 3209 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 <= 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

题意:用最少的矩形把n*m的矩阵填充满

题解:没思路搜索试一试

#include<bits/stdc++.h>
using namespace std;
struct node{
	vector<int>x,y;
}p[31][31];
int n,m,k;
int vis[31][31];
int ans;
void dfs(int mmp,int sum)
{
	if(sum>=ans) return;
	if(mmp==n*m)
	{
		ans=sum;
		return;
	}
	int x,y,flag=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			if(vis[i][j]==0)
			{
				x=i;y=j;flag=1;
				break;
			}
		}
		if(flag) break;
	}
	int xx,yy;
	for(int i=0;i<p[x][y].x.size();i++)
	{
		xx=p[x][y].x[i];
		yy=p[x][y].y[i];
		flag=1;
		for(int j=x;j<=xx;j++)
			for(int l=y;l<=yy;l++)
				if(vis[j][l])
					flag=0;
		if(flag)
		{
			for(int j=x;j<=xx;j++)
				for(int l=y;l<=yy;l++)
					vis[j][l]=1;
	//		cout<<x<<" "<<y<<endl;
			dfs(mmp+(xx-x+1)*(yy-y+1),sum+1);
			
			for(int j=x;j<=xx;j++)
				for(int l=y;l<=yy;l++)
					vis[j][l]=0;
		}
	}
}
int main()
{
	int T;
	int x1,x2,y1,y2;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&k);
		for(int i=0;i<=n;i++)
		{
			for(int j=0;j<=m;j++)
			{
				p[i][j].x.clear();
				p[i][j].y.clear();
				vis[i][j]=0;
			}
		}
		for(int i=1;i<=k;i++)
		{
			scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
			x1++,y1++;
			p[x1][y1].x.push_back(x2);
			p[x1][y1].y.push_back(y2);
		}
		ans=500;
		dfs(0,0);
		if(ans==500) ans=-1;
		printf("%d\n",ans);
		
	}
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值