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);
} }

最新文章

  1. 使用node.js生成excel报表下载(excel-export express篇)
  2. 【原创】Android selector选择器无效或无法正常显示的一点研究
  3. java 关键字 transient
  4. [LeetCode]题解(python):040-Combination Sum II
  5. &lt;input type=&quot;hidden&quot; id=&quot;haha&quot; name=&quot;wang&quot; value=&quot;xiaodong&quot; /&gt;
  6. DB2操作流程
  7. ORACLE之表
  8. Dedecms当前位置{dede:field name=&#39;position&#39;/}修改
  9. 二维数组在text,image的应用
  10. 前端开发的常用js库
  11. ion-scroll zooming=&quot;true&quot; android端无法缩放的问题
  12. jetty和tomcat的区别
  13. Linux如何挂载U盘
  14. luogu2774 [网络流24题]方格取数问题 (最小割)
  15. war包内更新文件
  16. CSV文件插入到mysql表中指定列
  17. Kotlin中常量和静态方法
  18. html基础整理(01居中 盒子问题)
  19. python的sorted函数对字典按value进行排序
  20. 【android】夜间模式简单实现

热门文章

  1. [LG1886]滑动窗口 单调队列
  2. BZOJ1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 【筛法】
  3. 停课day5
  4. 适用于实数范围的中缀表达式的 + - * / ( ) 计算(C++实现)
  5. springboot与dubbo结合
  6. Windows下安装Mycat-web
  7. 转:深入理解javascript原型和闭包系列
  8. sender的作用
  9. 【Mysql优化】key和index区别
  10. ubuntu安装mysqlclient