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. 花了几天学习了vue跟做的仿制app

    Vue.js国内开发者 是用于构建交互式的 Web  界面的库.它提供了mvvm 数据绑定和一个可组合的组件系统,具有简单.灵活的 API.从技术上讲, Vue.js 集中在 mvvm 模式上的视图模 ...

  2. ImportError libcublas.so.9.0

    What to do when you've installed cuda and tensorflow, but you get this error right after you import ...

  3. Linux安装TeamViewer

    1.下载teamview centos版本 官网只有rpm版本,附件中即为官网下载的teamview最新版本 (下载tar包方式,我这里打不开teamviewer的界面,所以这里不推荐) 官方下载地址 ...

  4. 解决eclipse的自动换行问题。

    安装方法:使用Eclipse 的自动升级功能,菜单栏选Help → install new Software 点解Add按钮,在“ Name ”中填入“ wordwrap ”,“ URL ”中填入“  ...

  5. error #10234-D: unresolved symbols remain error #10010: errors encountered during linking;

    error #10234-D: unresolved symbols remain error #10010: errors encountered during linking;: include ...

  6. vivado2015.4保存ila波形数据

    reference: https://www.cnblogs.com/pejoicen/p/d8b3c4f3aa29b8de7963893d4b99d361.html Vivado2015.4下,il ...

  7. 最近在学习Flask框架,那么就说下jinja2吧~~~

    jinja是组成Flask的模板引擎,先写一个demo吧 {% block body %} <ul> {% for user in users %} <li><a hre ...

  8. webpack-dev-server报错

    运行npm run dev报错,---- webpack-dev-server --inline --progress --config build/webpack.dev.conf.js npm E ...

  9. puthon文件头

    #!/usr/bin/u/ubv/a python # -*- coding:utf-8 -*-

  10. 运维seq语法2

    2017-03-02 09:47:42   # seq 1 10结果是1 2 3 4 5 6 7 8 9 10例二:#!/bin/bashfor i in `seq 1 10`;doecho $i;d ...