Rescue HDU1242 (BFS+优先队列)

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

本题题意是从‘a’搜到‘r’,‘.’为路1次为1秒,‘x’为卫兵,1次2秒,问从‘a’到‘r’用最短时间是多少。

处理时的方法是bfs,由于有卫兵的存在,队列的时间可能不是递增的,这样出队求最小值就可能不对,所以采取的是优先队列,每次让最短时间的出队。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;

struct node
{
    int x, y, tot;
    bool operator < (const node &a)const
    {
        return tot>a.tot;  
    }
};
int n, m;
node start;
int dir[4][2] = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
char mp[205][205];
int vis[205][205];
bool cheak(int x, int y)
{
    if (x >= 1 && x <= m&&y >= 1 && y <= n&&mp[x][y] != '#')
        return 1;
    else
        return 0;
}
int bfs()
{
    priority_queue<node>q;
    node cur, next;
    mp[start.x][start.y] = '#';
    q.push(start);
    while (!q.empty())
    {
        cur = q.top();
        q.pop();
        for (int i = 0; i < 4; i++)
        {
            next.x = cur.x + dir[i][0];
            next.y = cur.y + dir[i][1];
            next.tot = cur.tot + 1;
            if (cheak(next.x, next.y))
            {
                if (mp[next.x][next.y] == 'r')
                    return next.tot;
                else if (mp[next.x][next.y] == '.')
                {
                    mp[next.x][next.y] = '#';
                    q.push(next);
                }
                else if (mp[next.x][next.y] == 'x')
                {
                    mp[next.x][next.y] = '#';
                    next.tot++;
                    q.push(next);
                }

            }

        }

    }
    return -1;
}
int main()
{
    while (~scanf(" %d %d", &m, &n))
    {
        for (int i = 1; i <= m;i++)
        for (int j = 1; j <= n; j++)
        {
            scanf(" %c", &mp[i][j]);
            if (mp[i][j] == 'a')
            {
                start.x = i;
                start.y = j;
                start.tot = 0;
            }
        }
        int ans = bfs();
        if (ans+1)
        {
            printf("%d\n", ans);
        }
        else
            printf("Poor ANGEL has to stay in the prison all his life.\n");


    }
    return 0;
}

你可能感兴趣的:(搜索)