poj 1691 Painting A Board (构图 DFS)

Painting A Board
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3818 Accepted: 1890

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

矩形画板由带有颜色的小矩形组成,现在上色,要图某个小矩形需要满足在它上面的小矩形已经图过了,如果有和当前颜色相同的符合条件的小矩形就接着图,否则换画笔,问至少要拿起几次画笔。

思路:一个小矩形(看做点)和与它下相邻的小矩形可以构成有向图,并记录入度,这样开始只有入度为0的点可以涂色,当该点指向的与当前颜色相同的点也可以涂色,入度减1 ,否则换画笔,继续搜索。

code:

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct Node{
    int down,left,up,right;
    int color;
}node[30];
int ans;
int t,n,in[20],graph[30][30],vis[30];
void dfs(int num,int color1,int sum)//当前已经涂色的小矩形个数,当前颜色,当前拿笔次数
{
    if(sum>ans)
        return ;
    if(num==n)
    {
        if(sum<ans)
            ans=sum;
    }
    int i,j;
    for(i=0;i<n;i++)
    {
        if(in[i]==0&&!vis[i])
        {
            vis[i]=1;
           for(j=0;j<n;j++)
           {
               if(graph[i][j])
                in[j]--;
           }
            if(node[i].color==color1)
                dfs(num+1,color1,sum);
            else
                dfs(num+1,node[i].color,sum+1);
            //回溯、重置
            vis[i]=0;
            for(j=0;j<n;j++)
            {
                if(graph[i][j])
                    in[j]++;
            }
        }
    }
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int i;
        for(i=0;i<n;i++)
        {
            scanf("%d%d%d%d%d",&node[i].up,&node[i].left,&node[i].down,&node[i].right,&node[i].color);
        }
        memset(in,0,sizeof(in));
        memset(graph,0,sizeof(graph));
        int j;
        for(i=0;i<n;i++)//构图
        {
            for(j=0;j<n;j++)
            {
                if(node[i].down==node[j].up&&!(node[i].left>node[j].right||node[i].right<node[j].left))
                {
                    graph[i][j]=1;
                    in[j]++;
                }
            }
        }
        memset(vis,0,sizeof(vis));
        ans=n;
        dfs(0,0,0);//开始涂色0个,颜色无,拿笔0次
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值