Painting A Board----DFS

自动绘画机算法解析
本文介绍了一种自动绘画机的工作原理及其算法实现。通过合理的矩形区域划分与颜色分配,探讨如何最小化画板上不同颜色区域的绘制次数。文章详细解释了输入格式、输出要求及示例代码。

Painting A Board
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3814 Accepted: 1887

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


题目链接:http://poj.org/problem?id=1691


看了半天尴尬的发现还是没有看懂题......

看了题解的翻译,题意是说将一个大矩形划分成若干小矩形,告诉你每个小矩形的左上角那个点和右下角那个点的坐标,告诉你这个小矩形要涂的颜色,每个颜色对应一个刷子,问你最少要使用几次刷子。因为你要刷一个矩形之前,必须把这个矩形上方与之直接相邻的所有矩形先刷掉才能刷这个,如果你先用了红色的刷子,然后又用了蓝色的刷子,最后又用了红色的刷子,这算是3次使用而不是两次。

  举个例子来说,题目中的那张图,用红色刷B所以D也可以刷了,用蓝色先刷A,然后可以刷C,因为B刷了所以E也可以刷了,最后换刷子把剩下的刷掉,总共三次,这样对照的图看是不是比较明白。

有个很尴尬的地方需要注意一下,输入的五个数分别是ly,lx,ry,rx,color,是这个顺序,一开始输错了。。

这个题dfs来做就行,判断父节点的颜色是不是一样,至于上面有没有矩形就用拓扑序来判断,一看代码就明白了。


代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f
using namespace std;
struct node{
    int lx,ly,rx,ry,color;
}xin[100];
int n;
bool vis[100];
bool map1[20][20];
int degree[100];
int cnt;
void Build(){
    memset(vis,false,sizeof(vis));
    memset(map1,false,sizeof(map1));
    memset(degree,0,sizeof(degree));
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(i==j)
                continue;
            else if(xin[i].ly==xin[j].ry&&!(xin[i].rx<xin[j].lx||xin[i].lx>xin[j].rx)){
                map1[i][j]=true;
                degree[i]++;
            }
        }
    }
}
void dfs(int r,int ans,int step){
    if(ans>cnt)
        return ;
    if(step==n){
        cnt=ans;
        return ;
    }
    for(int i=0;i<n;i++){
        if(!vis[i]&°ree[i]==0){
            vis[i]=true;
            for(int j=0;j<n;j++){
                if(map1[j][i])
                    degree[j]--;
            }
            if(xin[i].color==r)
                dfs(r,ans,step+1);
            else
                dfs(xin[i].color,ans+1,step+1);
            vis[i]=false;
            for(int j=0;j<n;j++){
                if(map1[j][i])
                    degree[j]++;
            }
        }
    }
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++){
            scanf("%d",&xin[i].ly);
            scanf("%d",&xin[i].lx);
            scanf("%d",&xin[i].ry);
            scanf("%d",&xin[i].rx);
            scanf("%d",&xin[i].color);
        }
        Build();
        //for(int i=0;i<n;i++){
         //   for(int j=0;j<n;j++){
           //     printf("%d ",map1[i][j]);
            //}
            //cout<<endl;
        //}
        cnt=inf;
        dfs(0,0,0);
        cout<<cnt<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值