标签:
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:
- Color-code is an integer in the range of 1 .. 20.
- Upper left corner of the board coordinates is always (0,0).
- Coordinates are in the range of 0 .. 99.
- 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);
}
}
POJ 题目1691 Painting A Board(DFS)
标签:
原文地址:http://blog.csdn.net/yu_ch_sh/article/details/45821387