HDU-1242-Rescue(优先队列+BFS)

本文详细阐述了解决在充满障碍物和守卫的监狱中找到最短路径来救援天使的问题,通过使用广度优先搜索和优先队列的方法,实现了算法的高效求解。

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

H - Rescue
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 1242
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

广度搜索加优先队列,很常规的一道题
尝试了一下重载操作符
代码

#include<stdio.h>
#include<string.h>
#include<string>
#include<stack>
#include<queue>
#include<math.h>
#include<limits.h>
#include<iostream>
#include<algorithm>
using namespace std;
//又是广度搜索,优先队列
char map[202][202];//地图数据
int dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}}; //搜索方向
int visited[202][202];//已搜索标记为1,未搜索标记为0
int m,n;//地图大小
int starx,stary;//起始点坐标
struct node
{
    int x;
    int y;
    int time;
    friend bool operator < (const node &a,const node &b)//operator操作符重载
    {
        return a.time>b.time;//耗时少的排在队伍,先出列
    }
};
void BFS()
{
    priority_queue<node>q;//优先队列
    node star,end;
    star.x=starx;//记录起始点坐标
    star.y=stary;
    star.time=0;//初始化时间
    memset(visited,0,sizeof(visited));//初始化为未访问
    visited[star.x][star.y]=1;//起始点标记为已访问
    q.push(star);
    while(!q.empty())//优先队列
    {
        star=q.top();
        q.pop();
        if(map[star.x][star.y]=='r')//如果到达终点
        {
            printf("%d\n",star.time);
            return;
        }
        for(int i=0; i<4; i++)
        {
            end.x=star.x+dir[i][0];
            end.y=star.y+dir[i][1];
            if(end.x>=0&&end.x<m&&end.y>=0&&end.y<n&&map[end.x][end.y]!='#'&&visited[end.x][end.y]==0)//如果此结点合法
            {
                visited[end.x][end.y]=1;//标记此合法坐标为已访问
                if(map[end.x][end.y]=='x')//如果遇到特殊点
                    end.time=star.time+2;
                else
                    end.time=star.time+1;
                q.push(end);
            }
        }
    }
    printf("Poor ANGEL has to stay in the prison all his life.\n");//还有个句号要输出
}
int main()
{
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        for(int i=0; i<m; i++)
        {
            for(int j=0; j<n; j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='a')
                {
                    starx=i;
                    stary=j;
                    map[i][j]='#';
                }
            }
        }
        BFS();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值