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 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
随机推荐
- ID3、C4.5和cart算法比较(转)
转自:https://www.zhihu.com/question/27205203
- Typora快捷键的使用
无序列表:输入-之后输入空格 有序列表:输入数字+“.”之后输入空格 任务列表:-[空格]空格 文字 标题:ctrl+数字 表格:ctrl+t 生成目录:[TOC]按回车 选中一整行:ctrl+l 选 ...
- C#中货币类型和数值类型、字符串类型的转化
1.定义textbox的数据 private void Form1_Load(object sender, EventArgs e) { this.textBox1.Text = String.For ...
- ios 处理WKContentView的crash
http://www.jianshu.com/p/7ef5814a871b 解决WKContentView没有isSecureTextEntry方法造成的crash 程序中有web页面,使用W ...
- 【校招面试 之 剑指offer】第18题 删除链表中的节点
题目一:在O(1)时间内删除链表节点. 给定单项链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 思路:(1)如果要删除的节点不是链表的尾节点,则将被删除节点的内容复制到该节点,然 ...
- swift VFL - 父视图是scrollview 注意点
1. scrollview 添加 子控件, 必须设置 宽度和高度, 他自己无法撑满2. scrollview的子空间的约束 只能相对于 左边 的顶部, 无法设置右边和底部的约束 3. 设置最小距离 ...
- iOS - 工程文件冲突 - 解决方式
- ubuntu系统:插入优盘read-only file system
http://sharadchhetri.com/2013/12/19/how-to-fix-read-only-usb-pen-drive-in-ubuntu/ To fix USB pen dri ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
- 【原创】Silverlight的ComboBox.SelectValue无法赋值
前几天开发中 给ComboBox的SelectValue属性赋值是,老是赋不上去.之前SelectValue为Null,执行完调试看下,还是Null.很诡异 ComboBox的SelectVa ...