【POJ 1691】 Painting A Board(dfs)

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3582 Accepted: 1781

Description

The CE digital company has built an Automatic Painting Machine (APM) to paint a flat board fully covered by adjacent non-overlapping rectangles of different sizes each with a predefined color.

To color the board, the APM has access to a set of brushes. Each brush has a distinct color C. The APM picks one brush with color C and paints all possible rectangles having predefined color C with the following restrictions:
To avoid leaking the paints and mixing colors, a rectangle can only be painted if all rectangles immediately above it have already been painted. For example rectangle labeled F in Figure 1 is painted only after rectangles C and D are painted. Note that each rectangle must be painted at once, i.e. partial painting of one rectangle is not allowed.
You are to write a program for APM to paint a given board so that the number of brush pick-ups is minimum. Notice that if one brush is picked up more than once, all pick-ups are counted.

Input

The first line of the input file contains an integer M which is the number of test cases to solve (1 <= M <= 10). For each test case, the first line contains an integer N, the number of rectangles, followed by N lines describing the rectangles. Each rectangle R is specified by 5 integers in one line: the y and x coordinates of the upper left corner of R, the y and x coordinates of the lower right corner of R, followed by the color-code of R.
Note that:
  1. Color-code is an integer in the range of 1 .. 20.
  2. Upper left corner of the board coordinates is always (0,0).
  3. Coordinates are in the range of 0 .. 99.
  4. N is in the range of 1..15.

Output

One line for each test case showing the minimum number of brush pick-ups.

Sample Input

1
7
0 0 2 2 1
0 2 1 6 2
2 0 4 2 1
1 2 4 4 2
1 4 3 6 1
4 0 6 4 1
3 4 6 6 2

Sample Output

3

Source


题目大意:有一块画布,被分割成了n块矩形,每块矩形有自己的颜色,可能有某些部分颜色相同。
现在要对画布涂色,对应位置要涂成对应的颜色。
对要进行涂色的矩形有如下要求:在它上方的矩形必须已经被涂完。
每当选择一种颜色时,可以对所有要涂这种颜色且可以涂色的区域涂色。当切换另一种眼色时,上一种颜色会被清洗掉。

问最少需要多少色料(每次涂同种颜色时用掉一份色料

由于颜色最多只有20种。
可以把所有矩形区域存下,然后dfs搜索所有涂色顺序。
并且要记得对矩形排个序,以免先访问下面的矩形,可能此时无法对其涂色,但访问到其上面的矩形时,如果能涂色,该矩形也就能涂色了。
所以要提前从上往下按坐标排个序。

对于涂色,没有必要整个矩形区域涂色,只需要涂它的下界。因为只有这个才是有用的标记(下方的矩形需要依靠上方矩形的下界来判断是否可以被涂色

代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Range
{
	int x1,y1,x2,y2,c;
	bool f;
	bool operator < (const struct Range r)const
	{
		return x1 == r.x1? y1 < r.y1: x1 < r.x1;
	}
};

Range rg[33];
bool mp[111][111];
int n,mn;

void col(int pos)
{
	for(int i = rg[pos].y1; i <= rg[pos].y2; ++i)
		mp[rg[pos].x2][i] = 1;
}

bool can(int pos)
{
	if(rg[pos].x1 == 0) return true;

	for(int i = rg[pos].y1; i <= rg[pos].y2; ++i)
		if(!mp[rg[pos].x1][i]) return false;

	return true;
}

bool cal(int c,int *tmp,int &tp)
{
	tp = 0;

	for(int i = 0; i < n; ++i)
	{
		if(rg[i].f || rg[i].c != c) continue;
		if(can(i))
		{
			rg[i].f = 1;
			tmp[tp++] = i;
			col(i);
		}
	}

	return tp != 0;
}

void reset(int *tmp,int tp)
{
	for(int i = 0; i < tp; ++i)
	{
		rg[tmp[i]].f = 0;
		for(int j = rg[tmp[i]].y1; j <= rg[tmp[i]].y2; ++j)
			mp[rg[tmp[i]].x2][j] = 0;
	}
}

void dfs(int ok,int us)
{
	if(ok == n)
	{
		mn = min(mn,us);
		return;
	}

	int tmp[33];
	int tp;
	for(int i = 1; i <= 20; ++i)
	{
		if(!cal(i,tmp,tp)) continue;
		//printf("%d->%d %d %d\n",ok,ok+tp,us,i);
		dfs(ok+tp,us+1);
		reset(tmp,tp);
	}

}

int main()
{
	//fread();
	//fwrite();

	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i = 0; i < n; ++i)
		{
			scanf("%d%d%d%d%d",&rg[i].x1,&rg[i].y1,&rg[i].x2,&rg[i].y2,&rg[i].c);
			rg[i].f = 0;
		}
		sort(rg,rg+n);
		mn = INF;
		memset(mp,0,sizeof(mp));
		dfs(0,0);
		printf("%d\n",mn);
	}

	return 0;
}







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值