POJ 题目1691 Painting A Board(DFS)

Painting A Board

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3471   Accepted: 1723

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

Tehran 1999

题目大意:一个区域有n个矩形,给出,每个矩形的左上角坐标和右下角坐标,和它要图的颜色,每个刷子有一种颜色,当只有它上边所有和它挨着的矩形都图了,才能图他,问最少用几个刷子

ac代码

#include<stdio.h>
#include<string.h>
int n;
struct s
{
	int x1,y1,x2,y2,c;
}b[1001];
int map[1010][1010],dig[1010],vis[1010],ans,flag;
void dfs(int dep,int sum,int c)
{
	if(sum>ans)
		return;
	if(dep==n)
	{
	//	flag=1;
		ans=sum;
		return;
	}
	int i,j;
	for(i=0;i<n;i++)
	{
		if(!vis[i]&&dig[i]==0)
		{
			vis[i]=1;
			for(j=0;j<n;j++)
			{
				if(map[i][j])
					dig[j]--;
			}
			if(b[i].c==c)
				dfs(dep+1,sum,c);
			else
				dfs(dep+1,sum+1,b[i].c);
			for(j=0;j<n;j++)
			{
				if(map[i][j])
					dig[j]++;
			}
			vis[i]=0;
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		int i,j;
		for(i=0;i<n;i++)
		{
			scanf("%d%d%d%d%d",&b[i].y1,&b[i].x1,&b[i].y2,&b[i].x2,&b[i].c);
		}
		memset(map,0,sizeof(map));
		memset(dig,0,sizeof(dig));
		memset(vis,0,sizeof(vis));
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
			{
				if(b[i].y2==b[j].y1&&!(b[i].x2<b[j].x1||b[i].x1>b[j].x2))
				{
					dig[j]++;
					map[i][j]=1;
				}
			}
		}
		ans=0xfffffff;
		flag=0;
		dfs(0,0,0);
		printf("%d\n",ans);
	}
}
时间: 2024-08-28 20:48:31

POJ 题目1691 Painting A Board(DFS)的相关文章

POJ 1691 Painting A Board(DFS)

链接 题意 : 看了好长时间终于看懂题目了,将一个大矩形划分成若干小矩形,告诉你每个小矩形的左上角那个点和右下角那个点的坐标,告诉你这个小矩形要涂的颜色,每个颜色对应一个刷子,问你最少要使用几次刷子.因为你要刷一个矩形之前,必须把这个矩形上方与之直接相邻的所有矩形先刷掉才能刷这个,如果你先用了红色的刷子,然后又用了蓝色的刷子,最后又用了红色的刷子,这算是3次使用而不是两次,样例中,用红色刷B所以D也可以刷了,用蓝色先刷A,然后可以刷C,因为B刷了所以E也可以刷了,最后换刷子把剩下的刷掉,总共三次

[poj 1691] Painting A Board dfs+拓扑排序

Painting A Board Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3611 Accepted: 1795 Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat board fully covered by adjacent non-overlapping rectangle

POJ 1691 Painting A Board

题目大意: 墙上有一块区域被分成了n个矩形,每个矩形要涂上各自的颜色.为了保证完美要求这一块区域可以进行涂色的条件是它上方的所有区域都已经涂好颜色,这样就不会有后续的操作影响这块区域的颜色.但是如果两块区域颜色不同就要换涂颜色用的刷子.问最少需要换几次. 解题思路: 区域涂色的大体次序是由拓扑排序决定的,当有多个区域在同一层次时需要枚举这些区域来保证换刷子的次数最小. 下面是代码: #include <stdio.h> #include <stdlib.h> struct node

poj 1691 Painting A Board 拓扑序+dfs

题意: 一个木板上被分成了很多区域,每个区域要涂上一种特定的颜色,当涂一个区域的时候,它上方与它有重合部分的区域必须之前要涂好.求最少需要拿几次画笔(拿一次画笔可以涂颜色相同的多个区域). 分析: 上方与它有重合部分的区域必须之前要涂好这个限制可以用拓扑序来描述,每次涂有很多种可以涂的颜色,dfs就可以了.dfs的状态空间维数定为拿笔的次数,每次枚举可以涂的颜色.要注意的是dfs过程中要慎用全局变量..否则每个维度需枚举的东西被下一维改了就跪了... 代码: //poj 1691 //sep9

poj - 1691 - Painting A Board(状态压缩dp)

题意:N(1 <= N <= 15)个矩形,每个矩形要涂上指定的颜色C(1 <= C <= 20),如果给一个矩形涂色,那么与它相邻的上方矩形必须已经涂色,问最少要取几次画笔. 题目链接:http://poj.org/problem?id=1691 -->>状态:dp[S][color] 表示达到状态 S 且最后一次涂色为 color 时的最小取画笔数 状态转移方程:dp[S][color] = min(dp[S][color], dp[sub][i]); 或者 dp[

POJ - 1691 Painting A Board (状态压缩 + 暴力)

题目大意:要将一个大矩形內的所有小矩形涂色,涂色要求,只有该矩形上面的所有矩形都涂色了才可以涂该颜色,换一种填涂的颜色就要花费一点体力值,问填涂完需要花费的最小体力值 解题思路:先处理一下填涂该矩形的前提条件,我用了一个can数组表示填涂该矩形时要满足的状态量 用dp[color][state]表示当前用的颜色是color,填涂的状态为state时所用的最少体力值 然后暴力得出转换和结果 #include<cstdio> #include<cstring> #include<

POJ 题目1753 Flip Game(DFS)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33029   Accepted: 14435 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

POJ 2488 A Knight&#39;s Journey (DFS)

A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30656   Accepted: 10498 Description Background The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey ar

【POJ】3009 Curling 2.0 ——DFS

Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11432   Accepted: 4831 Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules are somewhat different from ours. The game is