Painting A Board
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
题意: 现在一个自动画板要用最少的画刷次数完成任务,
有一个要求就是:
每个小矩阵要染色的时候必须要先将 与其相邻和上面的小矩阵染色.
每个小矩阵给出左上角坐标和右下角坐 标和颜色的编号.
解题思路:
1. 染色问题. 先将图建立起来. 将当前小矩阵编号为i, 与其相邻的或则在上面的矩阵链接起来.(做标记)
2. 深搜解决. dfs(int reLen,int sum,int color)
reLen: 小矩阵已经染色的数目, sum: 当前使用画刷的次数. color: 当前画刷的颜色.
代码:
#include
<cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define MAX 20
const int INF = (1<<30);
struct node
{
int x1,
y1;
int x2,
y2;
int
color;
}rec[MAX];
int n;
bool vis[MAX], map[MAX][MAX];
int deg[MAX];
int result;
void read_graph()
{
for(int i =
0; i < n; ++i)
{
for(int j =
0; j < n; ++j)
{
if(rec[i].y2
== rec[j].y1 && (rec[j].x1
<= rec[i].x2 &&
rec[i].x1 <= rec[j].x2))
{
deg[j]++;
//第j个小矩阵有多少个前躯.
map[i][j] =
true;
}
}
}
}
void dfs(int reLen,int sum,int color)
{
if(sum
> result) return ;
if(reLen ==
n)
{
result =
sum;
return
;
}
for(int i =
0; i < n; ++i)
{
if(!vis[i]
&& deg[i] == 0)
//每次要从当前没有前躯的小矩阵开始染色.
{
vis[i] =
true;
for(int j =
0; j < n; ++j)
{
if(map[i][j]) //把小矩阵i的后继全部删除.
deg[j]--;
}
if(rec[i].color == color)
dfs(reLen+1,sum,color);
else
dfs(reLen+1,sum+1,rec[i].color);
vis[i] =
false;
for(int j =
0; j < n; ++j)
{
if(map[i][j]) //补回小矩阵i的后继
deg[j]++;
}
}
}
}
int main()
{
//
freopen("input.txt","r",stdin);
int
caseNum;
scanf("%d",&caseNum);
while(caseNum--)
{
memset(map,false,sizeof(map));
memset(deg,0,sizeof(deg));
memset(vis,false,sizeof(vis));
scanf("%d",&n);
for(int i =
0; i < n; ++i)
scanf("%d %d
%d %d
%d",&rec[i].y1,&rec[i].x1,&rec[i].y2,&rec[i].x2,&rec[i].color);
result =
INF;
read_graph();
dfs(0,0,0);
printf("%d\n",result);
}
return
0;
}