HDU - 1242 Rescue

本文介绍了一道经典的广度优先搜索(BFS)题目,通过引入优先队列解决了一个复杂场景下的路径寻找问题。该题目标是在一个由墙、道路和守卫组成的监狱地图中找到最短时间到达目标的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#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<node,vector<node>,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<<a.step<<endl;
            return;
        }
        if(a.x + 1 < n && 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.x - 1 >= 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."<<endl;
}

int main()
{
    while(cin>>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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值