【拓扑排序】【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 请给一个有向图着色,使得没有一个环只有一个颜色,您需要最小化使用颜色的数量. 思路 因为是有向图,每个环两 ...
随机推荐
- fileinput 小计(显示历史上传图片)
今天又需要,要求在选中某条记录后显示历史上传图片 上传控件是fileinput.js 想法:界面有上传图片的控件,重新加载控件,并加入历史上传图片地址 实现代码: var filepathArray ...
- 浅谈Trigger(SimpleTrigger&CronTrigger)
1.Trigger是什么 Quartz中的触发器用来告诉调度程序作业什么时候触发,即Trigger对象是用来触发执行job的. 2.Quartz中的Trigger 3.触发器通用属性: JobK ...
- Yii 1.1.17 三、数据库连接、定义模型、数据查询、验证登录、SESSION使用与URL生成
一.数据库连接 1.配置连接参数 在database.php里面开启: 'db' => array( 'connectionString' => 'mysql:host=127.0.0.1 ...
- HDU 6109 数据分割 并查集,SET
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6109 题意:中文题面 解法:每次都贪心地尝试将尽量多的条件放进当前这组,遇到第一个与已有条件冲突时,就 ...
- linux命令(29):cd命令
例1:进入系统根目录 cd / cd ../.. // [直接退到当前根目录] 例2:使用 cd 命令进入当前用户主目录 cd 例3:跳转到指定目录 cd /home/test 例4:返回进入此目 ...
- http通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤
http通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: (1) 建立TCP连接 在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连接是通过TCP来完成 ...
- Python 邮件发送消息
# 代码 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:supery import smtplib from email.mime.tex ...
- Django Ajax学习一
1. 简单的加法 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Restful Framework 初识
目录 一.什么是RESTful 二.什么是API 三.RESTful API规范 四.基于Django实现API 五.基于Django Rest Framework框架实现 一. 什么是RESTful ...
- 【转载】JAVA多线程和并发基础面试问答
转载自这里. 多线程和并发问题是Java技术面试中面试官比较喜欢问的问题之一.在这里,从面试的角度列出了大部分重要的问题,但是你仍然应该牢固的掌握Java多线程基础知识来对应日后碰到的问题.(校对注: ...