POJ 1691 Painting A Board(DFS)
题意 : 看了好长时间终于看懂题目了,将一个大矩形划分成若干小矩形,告诉你每个小矩形的左上角那个点和右下角那个点的坐标,告诉你这个小矩形要涂的颜色,每个颜色对应一个刷子,问你最少要使用几次刷子。因为你要刷一个矩形之前,必须把这个矩形上方与之直接相邻的所有矩形先刷掉才能刷这个,如果你先用了红色的刷子,然后又用了蓝色的刷子,最后又用了红色的刷子,这算是3次使用而不是两次,样例中,用红色刷B所以D也可以刷了,用蓝色先刷A,然后可以刷C,因为B刷了所以E也可以刷了,最后换刷子把剩下的刷掉,总共三次。
思路 : 这个题可以用DFS也可以用状压DP,我用的DFS,因为没压出来,,,,,,,这里有分析,链接1,链接2。先将每个矩形看成一个点,然后如果存在上下关系的话,下边那个点的入度+1,先找入度为0的点开始染色,如果这个点已经染掉了的话,那它下边的点入度要减1.。。。。。。。
//
#include <stdio.h>
#include <string.h>
#include <iostream> using namespace std ; struct rectangle
{
int lx,ly ;
int rx,ry ;
int R ;
} rec[];
int degree[] ;
bool mapp[][],vis[] ;
int M ,n;
int cnt ; void build()
{
memset(mapp,false,sizeof(mapp)) ;
memset(degree,,sizeof(degree)) ;
memset(vis,false,sizeof(vis)) ;
for(int i = ; i < n ; i++)
{
for(int j = ; j < n ; j++)
{
if(i == j) continue ;
else
{
if(rec[i].ly == rec[j].ry && !(rec[i].rx < rec[j].lx || rec[j].rx < rec[i].lx))
{
mapp[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 = ; i < n ; i++)
{
if(!vis[i] && degree[i] == )
{
vis[i] = true ;
for(int j = ; j < n ; j++)
if(mapp[j][i])
degree[j] -- ;
if(rec[i].R == r ) DFS(r,ans,step+) ;
else DFS(rec[i].R,ans + ,step+) ;
vis[i] = false ;
for (int j = ; j < n; j++)
{
if (mapp[j][i]) degree[j]++;
}
}
}
}
int main()
{
scanf("%d",&M) ;
while(M--)
{
scanf("%d",&n) ;
for(int i = ; i < n ; i++)
scanf("%d %d %d %d %d",&rec[i].ly,&rec[i].lx,&rec[i].ry,&rec[i].rx,&rec[i].R) ;
build() ;
cnt = ;
DFS(,,) ;
printf("%d\n",cnt) ;
}
return ;
}
POJ 1691 Painting A Board(DFS)的更多相关文章
- POJ 1691 Painting a Board(状态压缩DP)
Description The CE digital company has built an Automatic Painting Machine (APM) to paint a flat boa ...
- POJ 1691 Painting A Board(迭代深搜)
题目链接 调了一上午,单步的效率太低了,特别是在有递归的情况下...下午来了,输出调试了下,就发现bug了,各种混乱啊. 比较高兴的事,1Y了.本来还准备用edge1优化一下的,结果完全没用到.. # ...
- 【拓扑排序】【DFS】Painting A Board
[poj1691]Painting A Board Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3902 Accept ...
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- POJ 1979 Red and Black dfs 难度:0
http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...
- POJ 2378 Tree Cutting (DFS)
题目链接:http://poj.org/problem?id=2378 一棵树,去掉一个点剩下的每棵子树节点数不超过n/2.问有哪些这样的点,并按照顺序输出. dfs回溯即可. //#pragma c ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- poj 2531 Network Saboteur( dfs )
题目:http://poj.org/problem?id=2531 题意:一个矩阵,分成两个集合,求最大的 阻碍量 改的 一位大神的代码,比较简洁 #include<stdio.h> #i ...
随机推荐
- Django Form的学习
django.forms 是Django处理form的库 本质上可以直接通过对HttpRequest达到同样的效果,但是django.from带来更便捷的处理方式.功能有几点 通过form类 ...
- WPF自定义控件(三)——Window
一样!先来看看效果吧: 怎么样?效果很好吧,而且不只是样式哟!所有系统窗体有的交互操作都可以实现! 但可惜...有很多和系统API有关的东西本人了解得并不多,所以这个窗体是基于他人的成果上产生的.关于 ...
- hdu 1222 狼和兔子
Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...
- Linux挂载60T存储
操作系统: CentOS 6.3 存储:总大小为72T,并划分成3个块,每块20T 安装多实例MySQL数据库,不想挂载3个块,弄成一个大的比较方便管理,个人比较懒. 配置多路径:http://blo ...
- ios开发之NavBar和TarBar使用技巧
1 改变NavBar颜色:选中Navigation Bar 的Tint属性.选中颜色. 2 隐藏“back”按钮: self.navigationItem.hidesBackButton = YE ...
- PHP dirname() 函数 __FILE__ __DIR__
__DIR__返回文件所处的目录,除非是根目录,否则末尾不带\ __FILE__ 返回文件路径 dirname(__DIR__) 文件所处目录的上级目录,末尾也不带斜杠
- IIS本地服务器,设置IP地址问题
IIS启动本地网站的时候,设置了IP地址为本地IP,并绑定特定的端口号,电脑无法打开,但是同个局域网的手机可以打开.
- 简单的C#线程开发实例(隔一秒改变一下Label的Text)
要实现的效果:点击按纽,窗口上的label上出现1~100数字的变化. 第一个实例(把窗口上的label上文字改成0): using System; using System.Windows.Form ...
- LR(0)语法分析
# include <stdio.h> # include <string.h> //存储LR(0)分析表 struct node { char ch; int num; }; ...
- Android bluetooth low energy (ble) writeCharacteristic delay callback
I am implementing a application on Android using BLE Api (SDK 18), and I have a issue that the trans ...