Rescue HDU - 1242

问题:

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

大意:

#表示墙,“ . ”表示路,a表示天使,r是天使的朋友,x警卫。r要去找a,每走一步需要1分钟,打到警卫也需要一分钟,找到a的最短时间,如果找不到就输出"Poor ANGEL has to stay in the prison all his life." 

思路:

方法就是广搜。但因为走到“ . ”和x用的时间是不同的,所以我们要用到优先队列,让时间少的优先排列在队首。然后就是我用的提交代码的平台对与这个题,有一个小毛病(看代码)。

代码:

#define N 211
#include<queue>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char k[N][N];
int book[N][N];
int n,m,sx,sy;
int net[4][2]= {0,1,0,-1,1,0,-1,0};
struct node
{
    int x,y,step;
    bool operator < (const node &a)const   //优先队列重载" < ",
    {
        return a.step<step;//优先时间小的在队首
    }
};
void bfs()
{
    priority_queue<node>q;//优先队列的创建
    node h,t;
    h.x=sx;
    h.y=sy;
    h.step=0;
    book[h.x][h.y]=1;
    q.push(h);
    while(!q.empty())
    {
        h=q.top();
        q.pop();
        if(k[h.x][h.y]=='a')
        {
            printf("%d\n",h.step);
            return ;
        }
        for(int i=0; i<4; i++)
        {
            int tx=h.x+net[i][0];
            int ty=h.y+net[i][1];
            if(tx<0||ty<0||tx>=n||ty>=m||k[tx][ty]=='#'||book[tx][ty])
                continue;
            book[tx][ty]=1;
            if(k[tx][ty]=='x')   //如果是x,时间加2
                t.step=h.step+2;
            else
                t.step=h.step+1;
            t.x=tx;
            t.y=ty;
            q.push(t);
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");//找不到
    return;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(book,0,sizeof(book));
        for(int i=0; i<n; i++)
            scanf("%s",k[i]);
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(k[i][j]=='r')
                {
                    sx=i;//记录天使的朋友所在的位置
                    sy=j;
                    //bfs();       如果找到r就广搜,提交就是错误
                }
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lxxdong/article/details/81158068