【拓扑排序】【DFS】Painting A Board
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3902 | Accepted: 1924 |
Description

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
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
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);
}
}
【拓扑排序】【DFS】Painting A Board的更多相关文章
- ACM/ICPC 之 拓扑排序+DFS(POJ1128(ZOJ1083)-POJ1270)
两道经典的同类型拓扑排序+DFS问题,第二题较第一题简单,其中的难点在于字典序输出+建立单向无环图,另外理解题意是最难的难点,没有之一... POJ1128(ZOJ1083)-Frame Stacki ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- 拓扑排序-DFS
拓扑排序的DFS算法 输入:一个有向图 输出:顶点的拓扑序列 具体流程: (1) 调用DFS算法计算每一个顶点v的遍历完成时间f[v] (2) 当一个顶点完成遍历时,将该顶点放到一个链表的最前面 (3 ...
- Ordering Tasks(拓扑排序+dfs)
Ordering Tasks John has n tasks to do. Unfortunately, the tasks are not independent and the executio ...
- HDU 5438 拓扑排序+DFS
Ponds Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Sub ...
- POJ1128 Frame Stacking(拓扑排序+dfs)题解
Description Consider the following 5 picture frames placed on an 9 x 8 array. ........ ........ ... ...
- poj1270Following Orders(拓扑排序+dfs回溯)
题目链接: 啊哈哈.点我点我 题意是: 第一列给出全部的字母数,第二列给出一些先后顺序. 然后按字典序最小的方式输出全部的可能性.. . 思路: 整体来说是拓扑排序.可是又非常多细节要考虑.首先要按字 ...
- Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]
传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...
- 拓扑排序/DFS HDOJ 4324 Triangle LOVE
题目传送门 题意:判三角恋(三元环).如果A喜欢B,那么B一定不喜欢A,任意两人一定有关系连接 分析:正解应该是拓扑排序判环,如果有环,一定是三元环,证明. DFS:从任意一点开始搜索,搜索过的点标记 ...
- CodeForces-1217D (拓扑排序/dfs 判环)
题意 https://vjudge.net/problem/CodeForces-1217D 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
随机推荐
- Hadoop大数据生态系统及常用组件(山东数漫江湖)
经过多年信息化建设,我们已经进入一个神奇的“大数据”时代,无论是在通讯社交过程中使用的微信.QQ.电话.短信,还是吃喝玩乐时的用到的团购.电商.移动支付,都不断产生海量信息数据,数据和我们的工作生活密 ...
- esp8266 IOT Demo 固件刷写记录
将编译好的固件按照下面地址刷写到esp8266 出现下面错误是因为刷写的设置不对,按照图上设置: load 0x40100000, len 26828, room 16 tail 12chksum 0 ...
- java===字符串常用API介绍(转)
本文转自:http://blog.csdn.net/crazy_kid_hnf/article/details/55102861 字符串基本操作 1.substring(from,end)(含头不含尾 ...
- MySQL多线程复制故障(slave_pending_jobs_size_max)
MySQL多线程复制故障(slave_pending_jobs_size_max) http://www.xuchanggang.cn/archives/1079.html
- IIS配置PHP环境(快速最新版)(转载+自创)
(参考转载的) 我们知道php配置有几种: 1.CGI方式加载PHP环境,通常就是IIS里面配置解释器为php.exe,早期比较常见,目前使用较少. 特点是:稳定,但效率太低. 2.ISAPI方式加载 ...
- Java显式锁学习总结之一:概论
我们都知道在java中,当多个线程需要并发访问共享资源时需要使用同步,我们经常使用的同步方式就是synchronized关键字,事实上,在jdk1.5之前,只有synchronized一种同步方式.而 ...
- HDFS初识
参看原文 [Hadoop]HDFS的运行原理 参看原文 还不懂HDFS的工作原理?快来扫扫盲 简介 HDFS(Hadoop Distributed File System) Hadoop分布式文件系统 ...
- 用vue-cli来搭建vue项目和webpack
vue-cli 用vue-cli来搭建vue项目 第一步:全局安装vue-cli sudo npm install vue-cli -g 第二步:初始化一个项目 vue init webpack-si ...
- LeetCode解题报告—— Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [水煮 ASP.NET Web API2 方法论](1-7)CSRF-Cross-Site Request Forgery
问题 通过 CSRF(Cross-Site Request Forgery)防护,保护从 MVC 页面提交到ASP.NET Web API 的数据. 解决方案 ASP.NET 已经加入了 CSRF 防 ...