简单广搜:Rescue (优先队列)

本文介绍了一种使用优先队列实现的广度优先搜索算法来解决救援问题的方法。任务是在一个由墙壁、道路和守卫构成的监狱迷宫中找到最短路径以拯救角色Angel。文章提供了完整的C++代码实现,并通过具体的例子展示了算法的有效性。

摘要生成于 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



思路:这道题样例十分水,因为不用优先队列也可以过这组样例,所以为了避免其他人犯同样的错误,本博主就再弄一组样例:

输入:

7 8
a######r
........
#..#x...
..#..#.#
#...##..
.#......
........

输出:9

正确代码:

#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
char map[210][210];
int d[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
struct node
{
    int x,y,t;
} q,p;
struct cmp{
    bool operator () (node &a,node &b)
    {
        return a.t>b.t;
    }

};//优先队列模板。
bool check(int p,int pp)
{
    if(p<0||pp<0||p>=m||pp>=n||map[p][pp]=='#')
        return 0;
    return 1;
}
void bfs()
{
    priority_queue<node,vector<node>,cmp>que;//创建一个优先队列。最小值优先。
    que.push(q);
    while(!que.empty())
    {
        q=que.top();
        que.pop();
        for(int i=0; i<4; i++)
        {
            p.x=q.x+d[i][0];
            p.y=q.y+d[i][1];
            if(check(p.x,p.y))
            {
                if(map[p.x][p.y]=='x')
                    p.t=q.t+2;
                else
                    p.t=q.t+1;
                if(map[p.x][p.y]=='a')
                {
                    printf("%d\n",p.t);
                    return ;
                }
                que.push(p);
                map[p.x][p.y]='#';
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");
}
int main()
{
    while(~scanf("%d%d%*c",&m,&n))
    {
        memset(map,0x00,sizeof(map));
        for(int i=0; i<m; i++)
            scanf("%s",&map[i]);
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(map[i][j]=='r')
                {
                    q.x=i,q.y=j;
                    q.t=0;
                    map[i][j]='#';
                    break;
                }
            }
        }
        bfs();
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值