【poj 1691】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,2,1,那么要用3个刷子,但如果是1,1,2,那么只要2个刷子)
  求染完所有块所需最小刷子数

解:很明显的拓扑思想,网上有的题意题解太复杂
1.建图:建一条有向边从上方矩形指向相邻的下方矩形
模仿拓扑排序,in[]数组记录每个点入度
2.搜索:每次搜索条件是:入度为0,即上方所有矩形已经染          色,并且没有被染色过
细节不多,个人觉得建图比较难,代码如下:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node{int x1,x2,y1,y2;int id,c;} arr[30];
vector<int> lin[30];
int ans,n,T,in[30],vis[30];
int dfs(int tot,int step,int pre)
{
    if (step>=ans)  return 0;//简单的剪枝
    if (tot==n)
    {
        ans=step;return 0;
    }
    for (int i=1;i<=n;i++)//枚举每个能搜索的点
    if (vis[i]==0 && in[i]==0)
    {
        if (arr[i].c==pre) //如果上个颜色和这次一样,不用新的刷子
        {
            vis[i]=1;
            for (int j=0;j<lin[i].size();j++)
                in[lin[i][j]]--;
            dfs(tot+1,step,pre);
            vis[i]=0;
            for (int j=0;j<lin[i].size();j++)
                in[lin[i][j]]++;
            continue;
        }
        else//如果上个颜色和这次不一样,刷子数++
        {
            vis[i]=1;
            for (int j=0;j<lin[i].size();j++)
                in[lin[i][j]]--;
            dfs(tot+1,step+1,arr[i].c);
            vis[i]=0;
            for (int j=0;j<lin[i].size();j++)
                in[lin[i][j]]++;
            continue;
        }
    }
}



int main()
{
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        ans=99999999;
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d%d",&arr[i].y1,&arr[i].x1,&arr[i].y2,&arr[i].x2,&arr[i].c);
            arr[i].id=i;
            lin[i].clear();
            vis[i]=0;
            in[i]=0;
        }   
        //建图
        for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        if (arr[j].x1<arr[i].x2 && arr[j].x2>arr[i].x1 &&
            arr[j].y1==arr[i].y2)
        {
            lin[i].push_back(j);
            in[j]++;
        }
        //枚举每个能开始的点开始搜索
        for (int i=1;i<=n;i++)
        if (in[i]==0 && vis[i]==0)
        {
            vis[i]=1;
            for (int j=0;j<lin[i].size();j++)
                in[lin[i][j]]--;
            dfs(1,1,arr[i].c);
            vis[i]=0;
            for (int j=0;j<lin[i].size();j++)
                in[lin[i][j]]++;
        }
        printf("%d\n",ans);
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值