Painting A Board----DFS
来源:互联网 发布:面纱雾化器数据 编辑:程序博客网 时间:2024/06/05 20:58
Painting A Board
Time Limit: 1000MS Memory Limit: 10000KTotal 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.
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:
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
170 0 2 2 10 2 1 6 22 0 4 2 11 2 4 4 21 4 3 6 14 0 6 4 13 4 6 6 2
Sample Output
3
Source
Tehran 1999
题目链接: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 0x3f3f3f3fusing 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;}
0 0
- Painting A Board----DFS
- poj1691 Painting A Board(DFS+拓扑)
- poj1691--Painting A Board(拓扑+dfs)
- 【POJ 1691】 Painting A Board(dfs)
- [POJ 1691]Painting A Board[DFS][排序]
- poj_1691 Painting A Board(dfs+拓扑)
- poj 1691 Painting A Board (构图 DFS)
- pku 1691 painting a board dfs+回溯 解题报告
- POJ 1691 Painting A Board (DFS/状态DP)
- poj 1691 Painting A Board (拓扑排序+dfs)
- POJ 1691 Painting A Board(dfs搜索)
- poj 1691 Painting A Board(dfs,拓扑排序)
- poj 1691 Painting A Board 拓扑序+dfs
- POJ 题目1691 Painting A Board(DFS)
- [poj 1691] Painting A Board dfs+拓扑排序
- (POJ 1691)Painting A Board <top序列 + DFS>
- Painting A Board poj1691
- POJ1691--Painting A Board
- Java面向对象(二)重写(Override)与重载(Overload)
- 您需要安装OS X 10.10或OS X 10.11以及OS X server v4 或 OS X server v5
- Android 6.0权限封装整合
- 寻找和为定值的两个数
- 【线程】线程/多线程问题/线程池
- Painting A Board----DFS
- 人人都应该了解的十大算法
- 前端开源项目周报0207
- 题目1068:球的半径和体积
- leetcode [Palindrome Number]
- BP神经网络-总结
- 设计模式——构建器(Builder)模式
- Android 那些你所不知道的Bitmap对象详解
- 原型模式 prototype