G - Rescue 【地图型BFS+优先队列(有障碍物)】
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
InputFirst line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
OutputFor each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8
#.#####.
#.a#..r.
#..#x...
..#..#.#
#...##..
.#......
........
Sample Output
13
/*救人,
你在a,要救的人在r,
'.'是路可以走,'#'是墙,'x'是警卫,走路花费一秒,遇见警卫花费两秒。
问最小多少秒能救人,
能的话输出步数,不能的话输出"Poor ANGEL has to stay in the prison all his life."*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define ll long long
#define inf 0x3fffffff
using namespace std; const int maxn = ; int n,m,xxx,yyy;
char a[maxn][maxn];
int vis[maxn][maxn];
int dir[][]={ {,},{,},{-,},{,-} }; struct Node
{
int xx,yy,time; //横纵坐标+时间
friend bool operator < (Node a,Node b)
{
return a.time > b.time;
}
}; int check(int x,int y)
{
if(x<||x>=n||y<||y>=m||a[x][y]=='#'||vis[x][y])
return ;
return ;
}
/*int check(int x,int y)
{
if(x<0 || y<0 || x>=n || y>=m || !vis[x][y] || a[x][y] == '#')
return 1;
return 0;
}*/ int bfs(int x,int y)
{
memset(vis,,sizeof(vis));//清空标记数组放到bfs里面!!!!!
priority_queue<Node> q;
while(!q.empty()) q.pop();
q.push(Node{x,y,});
vis[x][y]=; while(!q.empty())
{
Node u=q.top();
q.pop();
Node tmp;
if(a[u.xx][u.yy]=='r')
return u.time;
for(int i=;i<;i++)
{
tmp.xx=u.xx+dir[i][];
tmp.yy=u.yy+dir[i][]; if( check(tmp.xx,tmp.yy) )
{
vis[tmp.xx][tmp.yy]=;
if(a[tmp.xx][tmp.yy]=='x')
tmp.time=u.time+;
else
tmp.time=u.time+;
q.push(tmp);
}
}
}
return -;
} int main()
{
memset(vis,,sizeof(vis));
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<n;i++)
{
scanf("%s",&a[i]);
} for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(a[i][j]=='a')
{
xxx=i;
yyy=j;
break;
}
}
} int ans=bfs(xxx,yyy);
if(ans==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
} }
G - Rescue 【地图型BFS+优先队列(有障碍物)】的更多相关文章
- C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】
4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...
- Rescue HDU1242 (BFS+优先队列) 标签: 搜索 2016-05-04 22:21 69人阅读 评论(0)
Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is describe ...
- D - Interesting Calculator 【数值型BFS+优先队列】
There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...
- F - Oil Deposits 【地图型BFS+联通性】
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- B - ACM小组的古怪象棋 【地图型BFS+特殊方向】
ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...
- HDU 1242 -Rescue (双向BFS)&&( BFS+优先队列)
题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...
- 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】
链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- POJ - 2312 Battle City BFS+优先队列
Battle City Many of us had played the game "Battle city" in our childhood, and some people ...
- hdu 5040 Instrusive【BFS+优先队列】
11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...
随机推荐
- POJ2155 Matrix 【二维线段树】
题目链接 POJ2155 题解 二维线段树水题,蒟蒻本想拿来养生一下 数据结构真的是有毒啊,, TM这题卡常 动态开点线段树会TLE[也不知道为什么] 直接开个二维数组反倒能过 #include< ...
- 【模拟赛·polyline】
Input file: polyline.in Output file: polyline.out Time limit: 1s Memory limit: 128M 有若⼲个类似于下⾯的函数: 定义 ...
- python3处理pdf
https://github.com/1049451037/pdfminer3k 使用pdfminer3k,如果是python2的话直接用pdfminer就行了. python setup.py in ...
- php 计算两个日期的间隔天数
使用php内部自带函数实现 1.使用DateTime::diff 实现计算 参考阅读>>PHP DateTime::diff() 上代码: <?php $start = " ...
- domReady的兼容性实现方法
一.为何要实现domReay方法? 举例: <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 'express' 不是内部或外部命令,也不是可运行的程序 或批处理文件。
新安装了express,但是当查看版本号输入: express -v 时出现如下错误: 网上查找了相关资料才发现express查看版本 的命令是 express -V (即V大写) 再次尝试: 发现同 ...
- 创建Maven项目出现:An internal error occurred during: "Retrieving archetypes:". Java heap space 错误解决办法
首先说明一下网上的方法: 在Eclipse中创建Maven的Web项目时出现错误:An internal error occurred during: "Retrieving archety ...
- 转:深入理解javascript原型和闭包系列
转自:深入理解javascript原型和闭包系列 从下面目录中可以看到,本系列有16篇文章,外加两篇后补的,一共18篇文章.写了半个月,从9月17号开始写的.每篇文章更新时,读者的反馈还是可以的,虽然 ...
- struts2学习问题(一)
一.struts2 Unknown tag (s:property). 解释:不识别标签 解决:这是sturts2的标签,导入相应的包<%@taglib prefix="s" ...
- [洛谷P1541] 乌龟棋
洛谷题目链接:乌龟棋 题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩 ...