HDU1242Rescue

本文介绍了一道典型的广度优先搜索(BFS)算法题目——解救被囚禁的天使。问题要求利用广搜找到从朋友位置出发到达天使位置所需的最短时间,途中需绕过墙壁并击败守卫。

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

Problem 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)想要去救他,路上会遇到障碍墙(#)和守卫(x),假设朋友一定可以打败守卫,且打败的时间为一单元,且朋友每走一步花费的时间也是一个单元,求出拯救天使最小的时间,如果不能救出就输出“Poor ANGEL has to stay in the prison all his life.”
解题思路:这次为一道基本的广搜题,但需要注意的是搜索时上下左右的排列顺序不同,则会导致不同的结果。

一组测试数据
3 3
#.r
#.x
#a#
如果按照上下左右的搜索顺序,则从r开始,搜索的下一个目标是(2,3)然后是(2,2)然后是(3,2),这样所到的最短时间是4,但是按照(1,3)->(1,2)->(2,2)->(3,2)的顺序来走得到最短时间是3,显然答案应该应该3,
我们可以使用优先队列来让时间最短的先出队,这样就可以解决以上问题,所以此题可以用优先队列加上广搜的方法。
源代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int N,M;
char ch[205][205];
int vis[205][205];
int x1,y1;
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
struct node
{
    int a,b,cns;
    friend bool operator<(node n1,node n2)  //时间最短的先出队
    {
        return n2.cns<n1.cns;
    }
};
void BFS(int x,int y)
{
    int yes=0;
    vis[x][y]=1;
    node p,q;
    p.a=x;
    p.b=y;
    p.cns=0;
    priority_queue<node> Q;    //定义优先队列
    Q.push(p);
    while(!Q.empty())
    {
        q=Q.top();   //一般队列取队首用front函数,而3优先队列取队首用top
        Q.pop();
        if(q.a==x1&&q.b==y1)
        {
            yes=1;
            cout<<q.cns<<endl;
            return;
        }
        for(int i=0;i<4;i++)
        {
            p.a=q.a+dir[i][0];
            p.b=q.b+dir[i][1];
            if(p.a>=1&&p.a<=N&&p.b>=1&&p.b<=M&&ch[p.a][p.b]!='#'&&vis[p.a][p.b]==0)
            {
                p.cns=q.cns+1;
                if(ch[p.a][p.b]=='x')
                    p.cns=p.cns+1;
                vis[p.a][p.b]=1;
                Q.push(p);
            }
        }
    }
    if(yes==0)
    cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        getchar();
        int x,y;
        int i,j;
        memset(ch,'0',sizeof(ch));
        memset(vis,0,sizeof(vis));
        for(i=1;i<=N;i++)
        {
            for(j=1;j<=M;j++)
            {
                scanf("%c",&ch[i][j]);
                if(ch[i][j]=='r')
                {
                    x=i;
                    y=j;

                }
                if(ch[i][j]=='a')
                {
                    x1=i;
                    y1=j;
                }
            }
            getchar();
        }
        BFS(x,y);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值