题目

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

题解

根据friend和friends,我们可以推知angel有多个朋友

也就是说,这是一个有一个入口多个出口的迷宫

从angel所在位置开始BFS,队列使用优先队列,确保按照距离层数访问节点

(第一次交蜜汁RE)

代码

/*
By:OhYee
Github:OhYee
Email:oyohyee@oyohyee.com
Blog:http://www.cnblogs.com/ohyee/

かしこいかわいい?
エリーチカ!
要写出来Хорошо的代码哦~
*/

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
using namespace std;

//DEBUG MODE
#define debug 0

//循环
#define REP(n) for(int o=0;o<n;o++)

const int maxn = 250;
int n,m;
char Map[maxn][maxn];

int s1,s2;

int dis[maxn][maxn];

const int delta[] = {1,-1,0,0};

struct point {
    int x,y;
    int dis;

    point(int a,int b,int c) {
        x = a;
        y = b;
        dis = c;
    }

    bool operator < (const point &rhs)const {
        return dis>rhs.dis;
    }
};

int BFS() {
    priority_queue<point> Q;
    memset(dis,-1,sizeof(dis));

    Q.push(point(s1,s2,0));
    dis[s1][s2] = 0;
    while(!Q.empty()) {
        int x = Q.top().x;
        int y = Q.top().y;
        int dist = Q.top().dis;

        Q.pop();

        REP(4) {
            int xx = x + delta[o];
            int yy = y + delta[3 - o];
            int dd = dist + 1;

            if(xx >= 0 && xx < n && yy>=0 && yy < m) {
                if(Map[xx][yy] != '#' && dis[xx][yy] == -1) {
                    if(Map[xx][yy] == 'x')
                        dd++;
                    if(Map[xx][yy] == 'r')
                        return dd;
                    dis[xx][yy] = dd;
                    Q.push(point(xx,yy,dd));
                }
            }
        }
    }
    return -1;
}

bool Do() {
    if(scanf("%d%d",&n,&m) == EOF)
        return false;
    for(int i = 0;i < n;i++)
        for(int j = 0;j < m;j++) {
            scanf("\n%c\n",&Map[i][j]);
            if(Map[i][j] == 'a') {
                s1 = i;
                s2 = j;
            }
        }

    int ans = BFS();
    if(ans == -1)
        printf("Poor ANGEL has to stay in the prison all his life.\n");
    else
        printf("%d\n",ans);

    return true;
}

int main() {
    while(Do());
    return 0;
}