(POJ 1691)Painting A Board <top序列 + DFS>

来源:互联网 发布:教育 大数据 知乎 编辑:程序博客网 时间:2024/06/05 19:52

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
Source

Tehran 1999

题意:
题意:
墙上有一面黑板,现划分为多个矩形,每个矩形都要涂上一种预设颜色C。

由于涂色时,颜料会向下流,为了避免处于下方的矩形的颜色与上方流下来的颜料发生混合,要求在对矩形i着色时,处于矩形i上方直接相邻位置的全部矩形都必须已填涂颜色。

在填涂颜色a时,若预设颜色为a的矩形均已着色,或暂时不符合着色要求,则更换新刷子,填涂颜色b。

1、 当对矩形i涂色后,发现矩形i下方的矩形j的预设颜色与矩形i一致,且矩形j上方的全部矩形均已涂色,那么j符合填涂条件,可以用 填涂i的刷子对j填涂,而不必更换新刷子。

2、 若颜色a在之前填涂过,后来填涂了颜色b,现在要重新填涂颜色a,还是要启用新刷子,不能使用之前用于填涂颜色a的刷子。

3、 若颜色a在刚才填涂过,现在要继续填涂颜色a,则无需更换新刷子。

4、 矩形着色不能只着色一部分,当确认对矩形i着色后,矩形i的整个区域将被着色

问最少要使用多少个刷子?

分析:
按照要求刷每个矩形,按照要求他们构成了一个top序列,我们只要按照top序列来刷矩形即可。
首先根据矩形的位置来构造出top序列,并记录每个矩形的入度和孩子。
然后每当刷子的颜色和当前可以刷的矩形的颜色都不一样时,要换刷子,且有多种可能,用c=0表示。
当刷子的颜色和当前可以刷的矩形的颜色一样时,则继续刷形同颜色的矩形。
利用DFS表示出所有的可能,并维护最小值即可。
DFS的参数:
x : 表示已经涮了的矩形的数目
c : 表示当前刷子的颜色,(c==0 时表示要重新选择)
times : 表示使用刷子的数目

AC代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int maxn = 20;struct rec{    int lx,ly,rx,ry,col; //左上,右下,颜色    int sonsnum,sons[maxn]; //孩子数,所有孩子    int rd;//入度}recs[maxn];bool vis[maxn];int n,ans;bool adj(int i,int j) // 判断是否满足top{    if(recs[i].lx >= recs[j].lx && recs[i].lx < recs[j].rx) return true;    if(recs[i].rx <= recs[j].rx && recs[i].rx > recs[j].rx) return true;    if(recs[i].lx <= recs[j].lx && recs[i].rx >= recs[j].rx) return true;    return false;}void init(){    ans = 20;    memset(vis,false,sizeof(vis));    for(int i=1;i<=n;i++)    {        recs[i].sonsnum = 0;        recs[i].rd = 0;    }    for(int i=1;i<=n;i++) //建立top序列    {        for(int j=1;j<=n;j++)        {            if(i != j && recs[i].ry == recs[j].ly)            {                if(adj(i,j))                {                    recs[j].rd++;                    recs[i].sonsnum++;                    recs[i].sons[recs[i].sonsnum] = j;                }            }        }    }}void cut(int x,int a)//修改入度数{    for(int i=1;i<=recs[x].sonsnum;i++)        recs[recs[x].sons[i]].rd += a;}void dfs(int x,int c,int times){    if(times >= ans) return; //剪枝    if(x ==n)    {        if(times < ans) ans = times;        return;    }    if(c == 0) //当前有多种选择    {        for(int i=1;i<=n;i++)        {            if(recs[i].rd == 0 && !vis[i]) //选一种            {                vis[i] = true;                cut(i,-1);                dfs(x+1,recs[i].col,times);                cut(i,1);                vis[i] = false;            }        }    }    else    {        bool flag = false;        for(int i=1;i<=n;i++)        {            if(recs[i].col == c && !vis[i] && recs[i].rd == 0)            {                flag = true;                vis[i] = true;                cut(i,-1);                dfs(x+1,c,times);                cut(i,1);                vis[i] = false;            }        }        if(!flag) //没有找到和当前刷子一个颜色的        {            dfs(x,0,times+1);        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        for(int i=1;i<=n;i++) scanf("%d%d%d%d%d",&recs[i].ly,&recs[i].lx,&recs[i].ry,&recs[i].rx,&recs[i].col);        init();        dfs(0,0,1);        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击