【poj 1691】Painting A Board
2016-04-11 15:38
519 查看
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); } }
相关文章推荐
- 开发错误记录5:Failed to resolve: com
- 调用 webservice 出现:WSDLReader:Loading of the WSDL file failed HRESULT=0×80040154: 没有注册类别 解决方案
- open(\"/dev/graphics/fb0\") failed!
- 自定义View时,用到Paint Canvas的一些温故,讲讲用路径绘画实现动画效果(基础篇 三)
- 自定义View时,用到Paint Canvas的一些温故,讲讲用路径绘画实现动画效果(基础篇 三)
- 配置Mailgraph_ext,使用Extman的图形日志
- LeetCode OJ 219.Contains Duplicate 2
- https请求 CFNetwork SSLHandshake failed (-9807) A connection failure occurred: SSL problem
- LeetCode OJ 217.Contains Duplicate
- Failed reading log event, reconnecting to retry
- Markov Chain(bate)
- 关于 Gradle failed: already disposed module 的问题
- maertSataDmorfnaideMdniF.295
- ZOJ 3780 Paint the Grid Again-贪心模拟/拓扑排序
- AIDL
- 下一代机器学习-在浏览器中训练深度学习模型Next Generation Machine Learning - Training Deep Learning Models in a Browser
- trail particle制作血管
- leetcode 11 Container With Most Water
- Argument list too long: recursive header expansion failed at
- RAID知识讲解