hdu 5040 bfs
http://acm.hdu.edu.cn/showproblem.php?pid=5040
一个人拿着纸盒子往目的地走 正常情况下一秒走一格 可以原地不动躲在盒子里 也可以套着盒子三秒走一格
地图上有些灯 灯能照到自己和面前一个格 每一秒灯顺时针转90度 如果要从灯照的地方离开或者进入灯照的地方就必须套上盒子
最短时间到达
题意不清的bfs
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <bitset>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 505;
int sx,sy,n;
int dx[] = {0,1,0,-1},
dy[] = {1,0,-1,0};
char s[maxn][maxn];
int dirr[128],notic[maxn][maxn];
bool vis[maxn][maxn][4];
bool in(int x,int y)
{
return 0 <= x && x < n && 0 <= y && y < n;
}
struct node{
int t,x,y;
bool operator < (const node &a)const{
return t > a.t;
}
};
int bfs()
{
priority_queue <node> q;
q.push((node){0,sx,sy});
while(!q.empty()){
node now = q.top(),to;
q.pop();
if(s[now.x][now.y] == 'T'){
return now.t;
}
if(vis[now.x][now.y][now.t%4]) continue;
vis[now.x][now.y][now.t%4] = true;
to = now,to.t++;
q.push(to);
for(int i = 0;i < 4;++i){
int mx = now.x + dx[i],my = now.y + dy[i];
if(in(mx,my) && s[mx][my] != '#'){
//所在格子和目的格子同一秒没有摄像头的时候才能走
to.t = now.t + 1;
if( (notic[mx][my] | notic[now.x][now.y]) & (1<<(now.t%4)) )
to.t = now.t + 3;
to.x = mx,to.y = my;
q.push(to);
}
}
}
return -1;
}
int main (){
int _,cas = 1;
RD(_);
dirr['E'] = 0,dirr['S'] = 1,dirr['W'] = 2,dirr['N'] = 3;
dirr['T'] = dirr['M'] = dirr['.'] = dirr['#'] = -1;
while(_--){
printf("Case #%d: ",cas++);
RD(n);
clr0(notic);
clr0(vis);
for(int i = 0;i < n;++i){
scanf("%s",s[i]);
for(int j = 0;j < n;++j){
if(s[i][j] == 'M')
sx = i,sy = j;
else{
int now = dirr[ s[i][j] ];
if(now == -1)
continue;
notic[i][j] = (1<<4) - 1;
for(int k = now;k < 4+now;++k){
int mx = i + dx[k%4],my = j + dy[k%4];
if(in(mx,my)){
notic[mx][my] |= (1<<(k-now));
}
}
}
}
}
cout<<bfs()<<endl;
}
return 0;
}
hdu 5040 bfs的更多相关文章
- hdu 5040 BFS 多维化处理图
http://acm.hdu.edu.cn/showproblem.php?pid=5040 跟这一题http://blog.csdn.net/u011026968/article/details/3 ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- 2014年北京网络赛 Instrusive HDU 5040 题解 优先队列
网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点, ...
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
- HDU 5040 Instrusive(BFS+优先队列)
题意比较啰嗦. 就是搜索加上一些特殊的条件,比如可以在原地不动,也就是在原地呆一秒,如果有监控也可以花3秒的时间走过去. 这种类型的题目还是比较常见的.以下代码b[i][j][x]表示格子i行j列在x ...
- HDU 5040
http://acm.hdu.edu.cn/showproblem.php?pid=5040 题意比较难懂,有摄像头的位置是可以走的,每回合开始看做人先走摄像头再转,也就是说如果你这回合走之前没有摄像 ...
- hdu 1430 (BFS 康托展开 或 map )
第一眼看到这题就直接BFS爆搜,第一发爆了内存,傻逼了忘标记了,然后就改,咋标记呢. 然后想到用map函数,就8!个不同的排列,换成字符串用map标记.然后又交一发果断超时,伤心,最恨超时,还不如来个 ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
随机推荐
- pandas 一行文本拆多行,一列拆多列
https://zhuanlan.zhihu.com/p/28337202 一列拆多列: http://blog.csdn.net/qq_22238533/article/details/761875 ...
- Spring工作原理与单例
最近看到spring管理的bean为单例的,当它与web容器整合的时候始终搞不太清除,就网上搜索写资料, Tomcat与多线程, servlet是多线程执行的,多线程是容器提供的能力. servlet ...
- 134. Gas Station (Array; DP)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- 几种常见的Web服务器
Apache与Tomcat的区别 ,几种常见的web/应用服务器 APACHE是一个web服务器环境程序 启用他可以作为web服务器使用 不过只支持静态网页 如(asp,php,cgi,jsp)等动态 ...
- 【c++】c++中重载输出操作符,为什么要返回引用
针对:ostream & operator <<(ostream & os, const ClassType &object) 说明几点: 1.第一个形参为对ost ...
- day7:vcp考试
Q121. An ESXi 6.x host in the vCenter Server inventory has disconnected due to an All Paths Down (AP ...
- VS Access DataSet 插入
在使用vs2008+access数据库,然后又使用了数据集,这时候插入操作遇到了问题,各种乱七八糟.各种头疼的问题就不说了,现在说找到的解决方法: 在xsd文件中插入TableAdapter后,会自动 ...
- java的PDF纵横向打印
PDF默认是纵向打印的,通过rotate()来让其改变为横向打印,一般在打印A4 12*21纸以及发票的时候会用横向打印.横向打印时页面会出现行转列以及列转行的情况,因此在设置页面大小的时候一定要宽度 ...
- Linux 下 FastDFS v5.08 分布式文件系统的安装
一.系统安装目录 源代码包目录 /data/wwwroot libevent安装目录 /usr/local/libevent FastDFS安装目录 /data/fastdfs nginx安装目录 / ...
- viewstamp replication: A new primary copy method to support highly-avaliable d
为了提高服务能力或者服务稳定,往往需要把数据重复布署,也就是replication.重复带来的问题是,更新的时候会带来不一致.一种比较简单的方法是,在N台重复的机器里选一台作为主机,其他作备份,只能通 ...