HDU - 1242 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.
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代表看守人员,每走一步花费1分钟,如果遇到x得浪费1分钟去杀死x也就是x花费2分钟,

这个题我错了好几遍简直错到怀疑人生,比赛进行到2/3的时候我突然想明白了,如果按照一般bfs写,依次入队列,每次都是队首先出队,然而这题因为有x

所以队列里的时间并不是按顺序排的,例如,现在已经入队一个点用时一分钟,改点的左侧a对应x,下方b对应.a一共用时3分钟先入队,b用一共时2分钟在入队

然后b的左侧,a的下侧的c本来是3分钟是最小的,但是a先出队,c未被标记所以c在a的基础上加一入队了,所以这题要先把队列里的时间排序,然后这个题就用到了优先队列

#include 
#include 
#include 
#include 
#define inf 0x3f3f3f3f

using namespace std;

typedef struct
{
    int x, y;
    int step;
} node;

struct //优先队列重载优先级
{
    bool operator()(const node a, const node b) const{
        return a.step > b.step;
    }
};

char map[500][500];
int v[500][500];
int n, m;
int k;
node p[500];

void bfs()
{
    node a, b;
    priority_queue,cmp>q;
    for(int i = 0; i < k; i++)
    {
        a.x = p[i].x;
        a.y = p[i].y;
        a.step = 0;
        v[a.x][a.y] = 1;
        q.push(a);
    }
    while(!q.empty())
    {
        a = q.top();
        q.pop();
        if(map[a.x][a.y] == 'a')
        {
            cout<= 0 && map[a.x-1][a.y] != '#' && !v[a.x-1][a.y])
        {
            b.x = a.x - 1;
            b.y = a.y;
            b.step = a.step + 1;
            if(map[b.x][b.y] == 'x')
                b.step++;
            v[b.x][b.y] = 1;
            q.push(b);
        }
        if(a.y + 1 < m && map[a.x][a.y+1] != '#' && !v[a.x][a.y+1])
        {
            b.x = a.x;
            b.y = a.y + 1;
            b.step = a.step + 1;
            if(map[b.x][b.y] == 'x')
                b.step++;
            v[b.x][b.y] = 1;
            q.push(b);
        }
        if(a.y - 1 >= 0 && map[a.x][a.y-1] != '#' && !v[a.x][a.y-1])
        {
            b.x = a.x;
            b.y = a.y - 1;
            b.step = a.step + 1;
            if(map[b.x][b.y] == 'x')
                b.step++;
            v[b.x][b.y] = 1;
            q.push(b);
        }
    }
    cout<<"Poor ANGEL has to stay in the prison all his life."<>n>>m)
    {
        int i, j;
        k = 0;
        memset(v,0,sizeof(v));
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < m; j++)
            {
                cin>>map[i][j];
                if(map[i][j] == 'r')
                {
                    p[k].x = i;
                    p[k++].y = j;
                }
            }
        }
        bfs();
    }
    return 0;
}


你可能感兴趣的:(HDU - 1242 Rescue)