hdu1072 Nightmare---BFS
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1072
题目大意:
在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器。定时炸弹的时间是6,人走一步所需要的时间是1。每次可以上、下、左、右移动一格。当人走到4时如果炸弹的时间不是0,可以重新设定炸弹的时间为6。如果人走到3而炸弹的时间不为0时,成功走出。求人从2走到3的最短时间。
思路:
从起点进行BFS,每个节点有四个属性,x,y,step(当前步数),time(剩余时间)
由于每个节点可以多次访问,所以BFS时的vis数组失效,但是每个炸弹重启器的点必须是第一次到达才是最优解,因为每次到该点,可以time变为6,可以再走6步,要是再次回来也只能和之前一样最多走6步,一定不是最优解。所以对每个炸弹重启点进行vis标记。每走一步step+1,time-1
#include<iostream>
#include<queue>
using namespace std;
int n, m;
int dir[][] = {,,,,-,,,-};
struct node
{
int x, y, time, step;//time表示炸弹剩余时间,step表示当前步数
node(){
}
node(int x, int y, int time, int step):x(x), y(y), time(time), step(step){
}
};
int x1, y1, x2, y2;
int Map[][];
bool judge(int x, int y)
{
return (x > && x <= n && y > && y <= m && Map[x][y] != );
}
int bfs()
{
queue<node>q;
node now(x1, y1, , );
q.push(now);
while(!q.empty())
{
node now = q.front();
q.pop();
//cout<<now.x<<" "<<now.y<<" "<<now.time<<" "<<now.step<<endl;
if(Map[now.x][now.y] == )
{
return now.step;
}
for(int i = ; i< ; i++)
{
int xx = now.x + dir[i][];
int yy = now.y + dir[i][];
if(!judge(xx, yy))continue;
int step = now.step + ;
int time = now.time - ;
if(time <= )continue;
if(Map[xx][yy] == )time = , Map[xx][yy] = ;//如果走到了时间调整器,就恢复时间,并且标记该点变成0,使得不能再走
q.push(node(xx, yy, time, step));
}
}
return -;
}
int main()
{
int T;
cin >> T;
while(T--)
{
cin >> n >> m;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= m; j++)
{
cin >> Map[i][j];
if(Map[i][j] == )
{
x1 = i, y1 = j;
}
if(Map[i][j] == )
{
x2 = i, y2 = j;
}
}
}
cout<<bfs()<<endl;;
}
return ;
}
hdu1072 Nightmare---BFS的更多相关文章
- HDU-1072 Nightmare (bfs+贪心)
Nightmare Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
Nightmare Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth w ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- HDUOJ-----(1072)Nightmare(bfs)
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- Nightmare BFS
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The la ...
- hdoj1072 Nightmare bfs
题意:在一个地图里逃亡,2是起点,3是终点,1是路,0是墙,逃亡者携带一个炸弹,6分钟就会炸,要在6分钟前到达4可以重制时间,问是否能逃亡,若能则输出最小值 我的思路:bfs在5步内是否存在3,存在则 ...
- HDU1072:Nightmare [DFS]
题目链接:Nightmare 题意: 给出一张n*m的图,0代表墙,1代表可以走,2代表起始点,3代表终点,4代表炸弹重置点 问是否能从起点到达终点 分析: 一道很好的DFS题目,炸弹重置点必然最多走 ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU1072:Nightmare
传送门 题意 给出一张n*m的图 0.墙 1.可走之路 2.起始点 3.终点 4.时间重置点 问是否能到达终点 分析 我的训练专题第一题,一开始我设个vis数组记录,然后写炸,不能处理重置点根vis的 ...
- hdu1072(Nightmare)bfs
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
随机推荐
- Linux开发环境工具收集
zsh & oh-my-zsh 配置oh-my-zsh之前要先安装Git sudo apt-get install zsh sudo apt-get install git wget http ...
- 动画:UIViewAnimationOptions类型
动画 1.常规动画属性设置(可以同时选择多个进行设置) UIViewAnimationOptionLayoutSubviews:动画过程中保证子视图跟随运动.**提交动画的时候布局子控件,表示子控件将 ...
- 【Darwin】 越狱后玩耍IPhone系统
玩耍IOS系统 大家都知道IOS是自Mac OS修改而来的.而Mac OS和IOS的共同核心是Darwin,其基于FreeBSD发展而来,整体而言也是个类Unix系统.之前把自己的手机越狱之后正好开始 ...
- 00_Linux介绍_我的Linux之路
原文章发布于特克斯博客www.susmote.com 什么是操作系统 操作系统(Operating System,简称OS)是管理和控制计算机硬件与软件资源的计算机程序,是直接运行在"裸机& ...
- HttpServletRequest对象方法的用法
1. 获得客户机信息 getRequestURL方法返回客户端发出请求时的完整URL. getRequestURI方法返回请求行中的资源名部分. getQueryString 方法返 ...
- JSON解析的几种方式
在开发中,网络请求和json解析使用的频率是一样高的,因为网络请求返回来的一般都是json(当然还有xml),这里讨论的是json,网络请求的工具类前面我的博客已经写过了,这里给出网址:http:// ...
- Hibernate学习笔记四 查询
HQL语法 1.基本语法 String hql = " from com.yyb.domain.Customer ";//完整写法 String hql2 = " fro ...
- node初始
### 一.什么是node.js > Node是一个基于 Chrome V8 引擎的 JavaScript 运行环境 > > Node使用了一个事件驱动.非阻塞式 I/O 的模型,使 ...
- 6块300G SCSI RAID5,两块硬盘损坏的数据恢复总结
[用户单位]XXXX网站[数据恢复故障描述]DELL POWEREDGE 2850服务器,内置6块300G SCSI硬盘 ,组成RAID5,安装LINUX REDHAT 4操作系统,存储大量照片,文件 ...
- 完美解决ubuntu Desktop 16.04 中文版firefox在非root用户不能正常启动的问题
ubuntu安装好后,默认安装有firefox浏览器,不过,非root的账户登录,双击firefox图标,居然出现如下提示:Your Firefox profile cannot be loaded. ...