Description

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

 

Input

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

 

Output

For 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
 
 #include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int m,n,i,j,ans,map[][],ex,ey,bx,by;
int dx[]={-,,,};
int dy[]={,,-,};
char str[];
struct stu
{
int x,y,step;
bool friend operator <(stu a,stu b)
{
return a.step>b.step;
}
}st;
int bfs()
{
priority_queue<stu>que;
int x,y,i,time;
stu next; st.x=bx;
st.y=by;
st.step=;
que.push(st);
while(!que.empty())
{ st=que.top();
que.pop();
time=st.step;
x=st.x;
y=st.y;
for(i=;i<;i++)
{
st.x=x+dx[i];
st.y=y+dy[i];
if(st.x>= &&st.y>=&&st.x<m&&st.y<m&&map[st.x][st.y]!=)
{
if(st.x == ex && st.y == ey)
{
return time+;
}
if(map[st.x][st.y] == )
{
st.step=time+;
}
else
{
st.step=time+;
}
map[st.x][st.y]=;
que.push(st);
}
}
}
return ;
}
int main()
{
while(scanf("%d %d",&m,&n)!=EOF)
{
memset(map,,sizeof(map));
for(i = ; i < m ; i++)
{
scanf("%s",&str);
for(j = ;j < n ; j++)
{
if(str[j] == '.')
{
map[i][j]=;
}
if(str[j] == 'x')
{
map[i][j]=;
}
if(str[j] == 'a')
{
map[i][j]=;
ex=i;
ey=j;
}
if(str[j] == 'r')
{
map[i][j]=;
bx=i;
by=j;
}
}
}
/* for( i = 0 ; i < m ; i++)
{
for(j =0 ; j < n ; j++)
{
printf("%d ",map[i][j]);
if(j == n-1)
{
printf("\n");
}
}
} */
map[bx][by]=;
ans=bfs();
if(ans)
printf("%d\n",ans);
else
printf("Poor ANGEL has to stay in the prison all his life.\n"); }
}

HDU 1242 Rescue 营救天使的更多相关文章

  1. hdu 1242 Rescue

    题目链接:hdu 1242 这题也是迷宫类搜索,题意说的是 'a' 表示被拯救的人,'r' 表示搜救者(注意可能有多个),'.' 表示道路(耗费一单位时间通过),'#' 表示墙壁,'x' 代表警卫(耗 ...

  2. 杭电 HDU 1242 Rescue

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 问题:牢房里有墙(#),警卫(x)和道路( . ),天使被关在牢房里位置为a,你的位置在r处,杀死一个警卫 ...

  3. hdu 1242:Rescue(BFS广搜 + 优先队列)

    Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submis ...

  4. hdu - 1242 Rescue && hdu - 2425 Hiking Trip (优先队列+bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1242 感觉题目没有表述清楚,angel的朋友应该不一定只有一个,那么正解就是a去搜索r,再用普通的bfs就能过了 ...

  5. hdu 1242 Rescue(bfs)

    此刻再看优先队列,不像刚接触时的那般迷茫!这也许就是集训的成果吧! 加油!!!优先队列必须要搞定的! 这道题意很简单!自己定义优先级别! +++++++++++++++++++++++++++++++ ...

  6. HDU 1242 Rescue(优先队列)

    题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description   Angel was caught by ...

  7. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

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

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

  9. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

随机推荐

  1. hdu1068 Girls and Boys 基础匈牙利

    #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...

  2. Educational Codeforces Round 24 A

    There are n students who have taken part in an olympiad. Now it's time to award the students. Some o ...

  3. JavaScript中简单排序总结

    JavaScript中简单排序总结 冒泡排序 经典排序算法, 双重for循环 在第二个for循环的时候, j < arr.len -1 -i , 这一步的优化很重要 function bullS ...

  4. Linux在线安装pip和numpy

    最近写Python需要用到numpy包 运行pip install numpy就会自动安装 一.因此需要先安装pip 1.如果安装的是Python>=2.7.9或者Python>=3.4, ...

  5. js去掉数组的空字符串

    后台返回数据的时候,有些数据为空时,一般都不进行显示,需要去除空字符串. 基本思路:获取数组张度,遍历数组,当数组某个值等于‘’或null或数据类型为undefined时,根据splice方法去除数据 ...

  6. Vue 2.0入门基础知识之全局API

    3.全局API 3-1. Vue.directive 自定义指令 Vue.directive用于自定义全局的指令 实例如下: <body> <div id="app&quo ...

  7. MSDN值得学习的地方

    作者:朱金灿 来源:http://blog.csdn.net/clever101 我一直认为:如果你没有乔布斯那样的天才,能够从头脑中原创出好产品,那么最好先学习分析好的产品,它到底好在哪里?哪些地方 ...

  8. 第一次向nodeclub提交修改

    今天第一次向nodeclub提交了两个修改,两个修改都是涉及部分浏览器的兼容性的. Manager很快合并了我的修改,好开森.晚上又重新回炉了一下git的日常操作,将git部分操作整理了一下写于此博. ...

  9. Knockout-了解Observable与computed

    KO是什么? KO不是万能的,它的出现主要是为了方便的解决下面的问题: UI元素较多,用户交互比较频繁,需要编写大量的手工代码维护UI元素的状态.样式等属性? UI元素之间关系比较紧密,比如操作一个元 ...

  10. Java遍历HashMap并修改(remove)

    遍历HashMap的方法有多种,比如通过获取map的keySet, entrySet, iterator之后,都可以实现遍历,然而如果在遍历过程中对map进行读取之外的操作则需要注意使用的遍历方式和操 ...