E - Rescue

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.


<b< dd="">

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

题意概括  :天使被关在一个迷宫里,天使的朋友想去救他,迷宫内.代表路,x代表警卫,a代表天使,r代表天使朋友所在的位置,每走一步需要消耗1单位时间,每消灭一个警卫需要一个单位时间,问最短多长时间能够营救到天使,如果不能输出“Poor ANGEL has to stay in the prison all his life.”.

解题思路  :首先找到天使朋友所在的位置,然后把每一个点都标记为false,广搜,先把起始点和时间压入队列,然后用一个while循环把队列内队首的量赋给b,然后删除队首的数据,继续把队首的数据赋给c,然后再删除一次,以此类推把时间也存入一个变量t内。接着判断是否到达天使的位置,是否遇见警卫,如果遇见警卫把警卫的位置变成点,时间加一,接着是四个方向的移动,最后如果不能到达天使的位置函数返回-1,最后判断函数是否为-1如果不是输出时间,如果不是输出"Poor ANGEL has to stay in the prison all his life."

 

#include<cstdio>

#include<queue>

using namespace std;

char a[200][201];
bool book[200][201];
int n,m,startx,starty;
const int next[4][2]={{-1,0},{1,0},{0,-1},{0,1} };

int bfs()
{
    queue<int> q;
    q.push(startx);
    q.push(starty);
    q.push(0);
    book[startx][starty] = true;
    
    while(!q.empty())
    {
        int b,c,t,k,tb,tc;
        b = q.front();
        q.pop();
        c = q.front();
        q.pop();
        t = q.front();
        q.pop();
        
        if(a[b][c] == 'a')
        {
            return t;
        }
        else if(a[b][c] == 'x')
        {
            a[b][c] = '.';
            q.push(b);
            q.push(c);
            q.push(t+1);
            continue;
        }
        for(k = 0;k<=3;k ++)
        {
            tb = b+next[k][0];
            tc = c+next[k][1];
            
            if(tb >= 0&&tb < n&&tc >= 0&&tc < m)
            {
                if(a[tb][tc] != '#'&&!book[tb][tc])
                {
                    book[tb][tc] = true;
                    q.push(tb);
                    q.push(tc);
                    q.push(t+1);
                    
                }
            }
        }
    }
    return -1;
}
int main()
{
    int i,j;
    
    while(~ scanf("%d%d",&n,&m))
    {
        for(i = 0;i < n;i ++)
        {
            scanf("%s",a[i]);
            for(j = 0;j < m;j ++)
            {
                if(a[i][j] == 'r')
                {
                    startx = i;
                    starty = j;
                }
                book[i][j] = false;
            }
        }
        int t = bfs();
        if(t == -1)
        printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
        printf("%d\n",t);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/y1356998843/article/details/81094401