搜索—Problem_1012-Rescue

本文介绍了解决Problem_1012-Rescue问题的方法,该问题要求寻找营救angle的最短时间。通过转换视角,使用优先队列和广度优先搜索(BFS)算法,实现了对所有可能路径的有效探索。

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

                             搜索—Problem_1012-Rescue

题意
营救angle,她被困在监狱里,监狱地形看作是N*M的矩阵,其中有墙,路以及卫兵。她的朋友们要去救她,只要其中任何一位朋友找到她即可。在营救过程中如果遇到卫兵需要杀死他才能通过继续前进,这种情况会用2个单位的时间,遇到墙不能走,其余的情况可以走,每移动一次消耗一个单位时间。如能营救成功,求营救的最小时间。否则输出“Poor ANGEL has to stay in the prison all his life.”
解题思路
采用BFS+优先队列。首先把问题从朋友们去营救angle转化为她去找朋友们,她可以找到任意一位朋友但所需要的时间是不同的,所以采用优先队列的方式,将找到朋友的时间排列,按最小时间进行搜索,如果顺着搜索下去可行就输出时间,不行的话换下一个路径,以此执行,实在无法完成就输出所给的话。
感想
与上课讲的例题类同,没讲之前是不容易想到用优先队列与BFS结合的,
AC代码

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
#define MAX 210

int n,m;
char map[MAX][MAX];
int visit[MAX][MAX];
int x1,x2,y1,y2;
int dir[4][2]={1,0,-1,0,0,1,0,-1,};

struct node{
    int x,y,step;
    friend bool operator<( node n1,node n2 )
    {
        return n2.step<n1.step;
    }
};

bool judge(int x,int y)
{
    if(x<0 || y<0 || x>=n || y>=m || !visit[x][y] || map[x][y] == '#')  return true;  
    return false; 
}

int FBS()
{
    int i;  
    priority_queue<node> Q;  
    node a,next;  
    a.x = x1;  
    a.y = y1;  
    a.step = 0;  
    Q.push(a);  
    visit[x1][y1] = 0;  
    while(!Q.empty())  
    {  
        a = Q.top();  
        Q.pop();  
        if(a.x == x2 && a.y == y2)  
            return a.step;  
        for(i = 0; i<4; ++i)  
        {  
            next = a;  
            next.x+=dir[i][0];  
            next.y+=dir[i][1];  
            if(judge(next.x,next.y))//判断  
                continue;  
            next.step++;  
            if(map[next.x][next.y] == 'x')//遇到卫兵处多花费一个单位时间  
                next.step++;  
            if(visit[next.x][next.y]>=next.step)//存入最小时间  
            {  
                visit[next.x][next.y] = next.step;  
                Q.push(next);  
            }  
        }  
    }  
    return 0;  
}  

int main()
{
    int i,j;
    while(cin>>n>>m)
    {
        for(i=0;i<n;++i)
        {
            cin>>map[i];
            for(j=0;map[i][j];++j)
            {
                if( map[i][j]=='r' )
                {
                    x1=i;
                    y1=j;
                }
                else if( map[i][j]=='a' )
                     {
                        x2=i;
                        y2=j;       
                     }  
            }
        }
        memset(visit,1,sizeof(visit));
        int ans=0;
        ans=FBS();
        if(ans) cout<<ans<<endl;
        else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; 
    }
}
PackagesNotFoundError: The following packages are not available from current channels: - zlib==1.2.13=h5eee18b_0 - xz==5.4.2=h5eee18b_0 - wheel==0.38.4=py38h06a4308_0 - tk==8.6.12=h1ccaba5_0 - tbb==2021.8.0=hdb19cb5_0 - sqlite==3.41.2=h5eee18b_0 - readline==8.2=h5eee18b_0 - python==3.8.17=h955ad1f_0 - pysocks==1.7.1=py38h06a4308_0 - pyopenssl==23.2.0=py38h06a4308_0 - pip==23.2.1=py38h06a4308_0 - openssl==3.0.10=h7f8727e_2 - ncurses==6.4=h6a678d5_0 - mpfr==3.1.2=0 - mpc==1.0.1=0 - mkl_random==1.2.2=py38h417a72b_1 - mkl_fft==1.3.6=py38h417a72b_1 - mkl-service==2.4.0=py38h5eee18b_1 - mkl==2023.1.0=h6d00ec8_46342 - libstdcxx-ng==11.2.0=h1234567_1 - libgomp==11.2.0=h1234567_1 - libgfortran5==11.2.0=h1234567_1 - libgfortran-ng==11.2.0=h00389a5_1 - libgcc-ng==11.2.0=h1234567_1 - libffi==3.4.4=h6a678d5_0 - ld_impl_linux-64==2.38=h1181459_1 - isl==0.17.1=0 - intel-openmp==2023.1.0=hdb19cb5_46305 - idna==3.4=py38h06a4308_0 - gmp==6.1.0=1 - gcc-6==6.1.0=2 - cudatoolkit==11.7.0=hd8887f6_10 - cryptography==41.0.2=py38h22a60cf_0 - cffi==1.15.1=py38h5eee18b_3 - ca-certificates==2023.7.22=hbcca054_0 - brotlipy==0.7.0=py38h27cfd23_1003 - _openmp_mutex==5.1=1_gnu Current channels: - https://conda.anaconda.org/omgarcia - https://conda.anaconda.org/conda-forge - https://repo.anaconda.com/pkgs/main - https://repo.anaconda.com/pkgs/r - https://repo.anaconda.com/pkgs/msys2 To search for alternate channels that may provide the conda package you're looking for, navigate to https://anaconda.org and use the search bar at the top of the page. 这是为什么
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值