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. CodeBlocks中我遇到的无法调试问题及解决方案

    CodeBlocks中遇到无法调试问题,可能有很多种,以下是我遇到的问题及解决方案 1.当无法调试时,如果IDE提示你出现下图中红色问题,说明你的调试器没有装好 2.在菜单栏点击Settings--D ...

  2. js的关于for的语句

    JavaScript for...in 语句 for...in 语句用于对数组或者对象的属性进行循环操作. for ... in 循环中的代码每执行一次,就会对数组的元素或者对象的属性进行一次操作. ...

  3. Struts2环境搭建和运用

    一.解压\struts-2.3.31\apps路径下struts2-blank.rar文件.将其中WEB-INFl路径下的lib中的包和web.xml文件复制到新项目中的WEB-INF路径下.web. ...

  4. react-redux-reducer

    reducer是对dispatch(action)的响应,是一个纯函数,接受旧的state和action,返回新的state. //纯函数要注意的点,下面的例子myname不变 <script& ...

  5. Shadow Properties之美(一)【Microsoft Entity Framework Core随笔】

    最近在做公司的项目的时候,开始把部分程序迁移到EF Core,然后有了一些感触,趁着还没忘却,还是先记录下来. EF Core还在成长中,我写这个的时候,版本是2.2.如果对着已有的EF 5/6来说, ...

  6. PC端车牌识别朱凯茵从事图像识别算法、OCR算法

    大家好,我是从事图像识别的pc端车牌识别朱凯茵,多多交流OCR算法,不限于车牌识别等,技术需要突破,你我成就梦想.

  7. nopcommerce 4.1 core 插件 相关1

    nop中 插件机制是比较值得学习的: Nop 插件学习: 1. 项目里面的生成必须是采用 直接编辑项目文件,参考nop原本的项目文件 动态加载插件的方法-mvc3 参考: using System.L ...

  8. spring的bean创建过程

    Spring的bean创建过程 步骤 执行过程 描述 1 ThreadLocal.set bean创建之前将beanName的一些属性放进ThreadLocal,避免多线程创建bean导致问题,并发创 ...

  9. Server Tomcat v7.0 Server at localhost failed to start.解决办法

    今天,导入maven项目时,报的错,因为之前没遇到过这个错,一时抓不到头绪,最后请技术大神帮忙解决.他首先看的eclipse的配置,是否与项目对应,在看看.seting 文件中的名称是否与项目名对应, ...

  10. jq解决a连接锚点平滑过渡

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...