Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

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+优先队列(有障碍物)】的更多相关文章

  1. C - 你经历过绝望吗?两次! 【地图型BFS+优先队列(障碍物)】

    4月16日,日本熊本地区强震后,受灾严重的阿苏市一养猪场倒塌,幸运的是,猪圈里很多头猪依然坚强存活.当地15名消防员耗时一天解救围困的“猪坚强”.不过与在废墟中靠吃木炭饮雨水存活36天的中国汶川“猪坚 ...

  2. 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 ...

  3. D - Interesting Calculator 【数值型BFS+优先队列】

    There is an interesting calculator. It has 3 rows of buttons. Row 1: button 0, 1, 2, 3, ..., 9. Pres ...

  4. F - Oil Deposits 【地图型BFS+联通性】

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

  5. B - ACM小组的古怪象棋 【地图型BFS+特殊方向】

    ACM小组的Samsara和Staginner对中国象棋特别感兴趣,尤其对马(可能是因为这个棋子的走法比较多吧)的使用进行深入研究.今天他们又在 构思一个古怪的棋局:假如Samsara只有一个马了,而 ...

  6. HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

    题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 開始以为是水题,想敲一下练手的,后来发现并非一个简单的搜索题,BFS做肯定出 ...

  7. 2018年长沙理工大学第十三届程序设计竞赛 G 逃离迷宫 【BFS】

    链接:https://www.nowcoder.com/acm/contest/96/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  8. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  9. hdu 5040 Instrusive【BFS+优先队列】

    11733274 2014-09-26 12:42:31 Accepted 5040 62MS 1592K 4848 B G++ czy 先转一个优先队列的用法: http://www.cppblog ...

随机推荐

  1. JavaScript获取访问设备信息

    <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>JavaScript获取访问设备信息</tit ...

  2. dhcp 和ntpdate时间同步

    为了防止路由器的dhcp服务干扰实验,我们2台机器分别新加了1快网卡. vmnet4 dhcp安装 [root@ygy130 ~]# yum -y install dhcp 将配置文件放在/etc/d ...

  3. CentOS 6通过yum升级Git

    By francis_hao    Mar 9,2017   在一个新机器上推送代码到github上时出现了下面的问题 error: The requested URL returned error: ...

  4. python3处理pdf

    https://github.com/1049451037/pdfminer3k 使用pdfminer3k,如果是python2的话直接用pdfminer就行了. python setup.py in ...

  5. Google File System中文版

    英文原文地址: Google File system 译文原文地址: The Google File System中文版 Google File System中文版 摘要 我们设计并实现了Google ...

  6. 自定义orderBy字母函数

    orderedUsers: function () { var arr = this.users; for (var i = 0; i < arr.length - 1; i++) { for ...

  7. CSS3学习笔记之径向展开菜单

    效果截图: HTML代码: <div class="menu-wrap"> <nav> <a href="" class=&quo ...

  8. MUI 按两次返回键退出应用 及 地理位置获取

    <span style="font-size:14px;"><span style="font-size:14px;"> mui.plu ...

  9. python最简单发送邮件

    #!/usr/bin/env python #coding:utf8 #Author:lsp #Date:下午5:51:13 #Version:0.1 #Function: #导入smtplib和MI ...

  10. [POJ1180&POJ3709]Batch Scheduling&K-Anonymous Sequence 斜率优化DP

    POJ1180 Batch Scheduling Description There is a sequence of N jobs to be processed on one machine. T ...