hdu2653

本文介绍了一种基于优先队列的迷宫寻路算法实现,通过定义节点结构体并利用BFS广度优先搜索策略,在限定时间内寻找从起点到终点的最短路径。文章详细解释了算法流程,并提供了完整的C++代码示例。

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

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string.h>
#include<stdio.h>
using namespace std;
char g[100][100];
bool vis[88][88][88];
int n,m,p,t,si,sj,ans;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
struct node
{
        int step,p,x,y;
        node(int a=0,int b=0,int c=0,int d=0):x(a),y(b),p(c),step(d){}
        bool friend operator <(const node a,const node b)
        {
                return a.step>b.step;
        }
};
void BFS()
{
        priority_queue<node> Q;
        node f=node(si,sj,p,0);
        Q.push(f);
        memset(vis,false,sizeof(vis));
        vis[si][sj][p]=true;
        node temp;
        while(!Q.empty())
        {
                temp=Q.top();
                Q.pop();
                if(temp.step>t)
                        return ;
                if(g[temp.x][temp.y]=='L')
                {
                        ans=temp.step;
                        return ;
                }
                for(int k=0;k<4;k++)
                {
                        int i=dir[k][0]+temp.x;
                        int j=dir[k][1]+temp.y;
                        if(i<0||i>n-1 || j<0 || j>m-1||g[i][j]=='#')
                                continue;
                        if(temp.p!=0 && !vis[i][j][temp.p-1])
                        {
                                vis[i][j][temp.p-1]=true;
                                Q.push(node(i,j,temp.p-1,temp.step+1));
                        }
                        if(g[temp.x][temp.y]!='@' && g[i][j]!='@'&&!vis[i][j][temp.p])
                        {
                                vis[i][j][temp.p]=true;
                                Q.push(node(i,j,temp.p,temp.step+2));
                        }
                }
        }
        return ;
}
int main()
{
        int cas=0;
        while(scanf("%d %d %d %d",&n,&m,&t,&p)==4)
        {
                for(int i=0;i<n;i++)
                {
                        scanf("%s",g[i]);
                        for(int j=0;j<m;j++)
                        if(g[i][j]=='Y')
                                si=i,sj=j;
                }
                ans=100001;
                BFS();
                printf("Case %d:\n",++cas);
                if(ans>t)
                        printf("Poor Yifenfei, he has to wait another ten thousand years.\n");
                else
                        printf("Yes, Yifenfei will kill Lemon at %d sec.\n",ans);
        }
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值