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 <iostream>
#include <algorithm>
#include <queue>
#include <cstring> using namespace std;
const int maxn=200+10;
const int inf=99999999;
int vis[maxn][maxn];
int mp[maxn][maxn];
int sx,sy,ex,ey;
int ans;
int n,m,flag;
int dis[4][2]={-1,0,0,1,1,0,0,-1};
struct node {
int x,y;
int step;
};
int check(int x,int y) {
if(x<1||y<1||x>n||y>m) return 1;
//if(vis[x][y]) return 1;
if(mp[x][y]==0) return 1;
return 0;
} void bfs() {
node a,next;
a.x=sx;
a.y=sy;
a.step=0;
vis[sx][sy]=0;
queue<node> q;
q.push(a);
while(!q.empty()) { a=q.front();
// cout<<a.x<<" "<<a.y<<endl;
q.pop();
if(a.x==ex&&a.y==ey) {
if(ans>a.step) {
flag=1;
ans=a.step;
}
}
for(int i=0;i<4;i++) {
next.x=a.x+dis[i][0];
next.y=a.y+dis[i][1];
if(check(next.x,next.y))
continue;
if(mp[next.x][next.y]==1) next.step=a.step+1;
if(mp[next.x][next.y]==2) next.step=a.step+2;//bfs并不是等距的,造成某些短路被提前走过
if(vis[next.x][next.y]>a.step) {
q.push(next);
vis[next.x][next.y]=a.step;
} }
//cout<<a.x<<" "<<a.y<<endl;
}
}
int main() {
// freopen("input.txt","r",stdin);
while(scanf("%d%d\n",&n,&m)!=EOF) {
flag=0;
char ch[300];
ans=99999999999;
memset(mp,0,sizeof(mp));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
vis[i][j]=inf;
}
for(int i=1;i<=n;i++) {
scanf("%s",ch);
for(int j=0;j<m;j++) {
if(ch[j]=='.') mp[i][j+1]=1;
if(ch[j]=='x') mp[i][j+1]=2;
if(ch[j]=='r') {
mp[i][j+1]=1;
sx=i;
sy=j+1;
}
if(ch[j]=='a') {
mp[i][j+1]=1;
ex=i;
ey=j+1;
}
}
}
bfs();
if(flag) printf("%d\n",ans);
else printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}

zoj 1649 bfs的更多相关文章

  1. BFS zoj 1649

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 //hnldyhy(303882171) 11:12:46 // z ...

  2. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  3. zoj 1649 Rescue (BFS)(转载)

    又是类似骑士拯救公主,不过这个是朋友拯救天使的故事... 不同的是,天使有多个朋友,而骑士一般单枪匹马比较帅~ 求到达天使的最短时间,杀死一个护卫1 units time , 走一个格子 1 unit ...

  4. ZOJ 1649:Rescue(BFS)

    Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put ...

  5. ZOJ 1649 Rescue(有敌人迷宫BFS)

    题意 求迷宫中从a的位置到r的位置须要的最少时间  经过'.'方格须要1s  经过'x'方格须要两秒  '#'表示墙 因为有1s和2s两种情况  须要在基础迷宫bfs上加些推断 令到达每一个点的时间初 ...

  6. zoj 2081 BFS 延迟标记 读入问题

    Mission Impossible Time Limit: 2 Seconds                                     Memory Limit: 65536 KB  ...

  7. zoj 1649 Rescue

    BFS..第一次使用C++ STL的队列来写广搜. #include<stdio.h> #include<string.h> #include<math.h> #i ...

  8. zoj 1649

    #include <iostream> #include <queue> using namespace std; int n,m,s2,e2; int b[205][205] ...

  9. HZNU Training 1 for Zhejiang Provincial Collegiate Programming Contest

    赛后总结: TJ:今天我先到实验室,开始看题,一眼就看了一道防AK的题目,还居然觉得自己能做wwww.然后金姐和彭彭来了以后,我和他们讲了点题目.然后金姐开始搞dfs,我和彭彭看榜研究F题.想了很久脑 ...

随机推荐

  1. 低版本IDE 打开 高版本 IDE 代码时 unit

    可以用单元别名 比如Vcl.Forms=Forms 来兼容.

  2. shell 下生成使用UUID

    #!/bin/bash psd="/proc/sys/kernel/random/uuid" echo $(cat $psd)UUID=$(cat /proc/sys/kernel ...

  3. JAVAEE第四周

    一.定义 Async函数是Generator函数的语法糖,但相较来说比Generator函数更强大一些.主要用于1解决一次异步调用异步函数的问题,即当第一个异步调用结束后,再调用第二个异步函数:等第二 ...

  4. 小程序获取openid 出现null,{"errcode":40163,"errmsg":"code been used, hints: [ req_id: WNUzlA0105th41 ]"}

    //根据微信提供的接口,请求得到openid和session_id public class UserInfoUtils { private String getKeyURL="https: ...

  5. java 随机生成6位短信验证码

    生成6位随机数字其实很简单,只需一行代码,具体如下: String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000) ...

  6. union、union all 、distinct的区别和用途

    1.从用途上讲 它们都具有去重的效果 2.从效率上讲 distinct通常不建议使用,效率较低;union all 和union 而言,union all效率更高;原因是:union 相当于多表查询出 ...

  7. clusterware启动顺序——OHASD

      Clusterware启动顺序 [root@ebsdb1 etc]# crsctl check crs CRS-4638: Oracle High Availability Services is ...

  8. element-ui(或者说Vue的子组件)绑定的方法中传入自定义参数

    比如el-upload中的 :on-success= fn,其实是给组件el-upload传递一个prop,这样写的话fn只能接受upload组件规定的参数,如果想自己传递父组件中的参数比如b,要写成 ...

  9. Git通过密钥对远程仓库上传和更新详细操作

    1,先到ssh中ls查看之前本地生成的公钥和私钥,然后将别人的密钥替换掉自己的密钥,这里我把别人的密钥放在d:/desktop/id_rsa 中,利用cp /D/Desktop/id_rsa  id_ ...

  10. MySQL数据库需进行修改密码问题解决方案

    两种方式可供大家进行参考: 第一种: 格式:mysqladmin -u用户名 -p旧密码 password 新密码 1.给root加个密码pass123: 首先在DOS下进入目录mysql\bin,然 ...