[poj1691]Painting A Board
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3902   Accepted: 1924

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

 
题目大意:给一个板子,涂下面的板子前要先涂完上面所有与之相邻的。同一种画笔拿起一次算一次使用,请问最少多少次涂完画板?
试题分析:建图,求所有的拓扑序后每个拓扑序暴力求解次数,找最少的就好了
 
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int T;
bool dis[41][41]; int x1[41],y1[41],x2[41],y2[41],col[41];
int N,res,ans=999999;
int tu[41];
bool vis[41];
int que[41]; void dfs(int st){
if(st==N) {
res=0;
for(int k=1;k<=N;k++) if(que[k]!=que[k-1]) res++;
ans=min(ans,res);
return ;
}
for(int i=1;i<=N;i++){
if(!tu[i]&&!vis[i]){
for(int j=1;j<=N;j++) if(dis[i][j]) tu[j]--;
vis[i]=true;que[st+1]=col[i];
dfs(st+1);
vis[i]=false;
for(int j=1;j<=N;j++) if(dis[i][j]) tu[j]++;
}
}
return ;
} int main(){
T=read();
while(T--){
ans=999999;
memset(tu,0,sizeof(tu));
memset(dis,false,sizeof(dis));
N=read();
for(int i=1;i<=N;i++){
x1[i]=read(),y1[i]=read();
x2[i]=read(),y2[i]=read();
col[i]=read();
}
for(int i=1;i<=N;i++){
for(int j=1;j<=N;j++)
if(i!=j&&x2[j]==x1[i]&&((y1[i]>=y1[j]&&y1[i]<=y2[j])||(y2[i]<=y2[j]&&y2[i]>=y1[j])||(y1[i]<=y1[j]&&y2[i]>=y2[j])||(y1[i]>=y1[j]&&y2[i]<=y2[j]))) dis[j][i]=true,tu[i]++;
}
dfs(0);
printf("%d\n",ans);
}
}

最新文章

  1. IOS线程的一些总结
  2. groovy-实现接口
  3. Javascript操作剪切板数据(支持IE、Chrome、360、搜狗),亲测!
  4. Html5——地理定位及地图
  5. JAVA FILE or I/O学习 - File学习
  6. 北风风hadoop课程体系
  7. Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
  8. K60平台智能车开发工作随手记
  9. jquary高级和ajax
  10. 一个解决过程:Servlet [某路径xxx] in web application [/项目xxx] threw load() exception和java.lang.ClassNotFoundException XXX
  11. java学习之—并归排序
  12. table-layui
  13. DRF的解析器和渲染器
  14. sublime Text 几款插件
  15. VS2010自带的性能分析工具分析.NET程序的性能
  16. fzu2157(树形dp)
  17. hdu1233 还是畅通工程 最小生成树
  18. IOS网络访问详解
  19. PAT 1139 First Contact[难][模拟]
  20. 搭建Hexo博客系统

热门文章

  1. 转一篇sublime必备的一些插件
  2. apache log 按日期记录 格式 &lt;GOOD&gt;-- (转)
  3. hdfs基本思想
  4. wait与waitpid
  5. Linux内核【链表】整理笔记(2) 【转】
  6. 中断中处理延时及一些函数的调用规则(中断调i2c驱动有感)--中断中的延迟delay与printk函数的冲突【转】
  7. $scope作用及模块化解决全局问题
  8. pycaffe做识别时通道转换问题
  9. dev....把pivotgridview和chart一起导出
  10. 数据类型转换(计算mac地址)