HDU 3345 bfs

本文介绍了一种基于优先队列的迷宫寻路算法实现,通过C++代码详细展示了如何在给定迷宫地图中寻找从特定起点到终点的有效路径。文章探讨了不同地形对移动次数的影响,并通过广度优先搜索(BFS)变体来解决这一问题。

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

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

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

int n,m,mv;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
char graph[105][105];  //由于不需要回溯,所以可以直接在原来的地图上进行标记
bool vis[105][105];
int sx,sy;

bool judge(int x, int y){
    for(int i=0;i<4;i++){
        int xx = x+dx[i];
        int yy = y+dy[i];
        if(xx>=0 && xx<n && yy>=0 && yy<m &&graph[xx][yy]=='E'){
            return true;
        }
    }
    return false;
}

void bfs(){
    priority_queue<node> q;
    node cur,next;
    vis[sx][sy] = 1;
    cur.x = sx;
    cur.y = sy;
    cur.mv = mv;
    q.push(cur);
    while(!q.empty()){
        cur = q.top();
        q.pop();
        if(cur.mv <= 0) continue;
//        if(judge(cur.x,cur.y) && graph[cur.x][cur.y]!='Y')
//            cur.mv = 0;
        if(cur.mv==0) continue;
        for(int i=0;i<4;i++){
            int xx = cur.x+dx[i];
            int yy = cur.y+dy[i];
            if(xx>=0 && xx<n && yy>=0 && yy<m && !vis[xx][yy] && graph[xx][yy]!='#' && graph[xx][yy]!='*' && graph[xx][yy]!='E'){
                next.x = xx;
                next.y = yy;
                next.mv = cur.mv;
                char ch = graph[xx][yy];
                if(ch=='.' || ch=='P')
                    next.mv--;
                else if(ch=='T')
                    next.mv -= 2;
                else if(ch=='R')
                    next.mv -=3;
                if(next.mv<0) continue;
                if(judge(next.x,next.y)) next.mv = 0;
                q.push(next);
                vis[next.x][next.y] = 1;
                if(graph[next.x][next.y] != 'P')
                    graph[next.x][next.y] = '*';
                
                
//                if(graph[xx][yy] == '.'){  //1
//                    next.mv = cur.mv-1;
//                    graph[xx][yy] = '*';
//                    if(next.mv>0){
//                        vis[xx][yy] = 1;
//                        q.push(next);
//                    }
//
//                }
//                else if(graph[xx][yy] == 'T'){  //2
//                    next.mv = cur.mv-2;
//                    graph[xx][yy] = '*';
//                    if(next.mv>0){
//                        vis[xx][yy] = 1;
//                        q.push(next);
//                    }
//                }
//                else if(graph[xx][yy] == 'R'){  //3
//                    next.mv = cur.mv-3;
//                    graph[xx][yy] = '*';
//                    if(next.mv>0){
//                        vis[xx][yy] = 1;
//                        q.push(next);
//                    }
//                }
//                else if(graph[xx][yy] == 'P'){
//                    next.mv = cur.mv-1;
//                    if(next.mv>0){
//                        vis[xx][yy] = 1;
//                        q.push(next);
//                    }
//                }
            }
        }
    }
}

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&n,&m,&mv);
        for(int i=0;i<n;i++)
            scanf("%s",graph[i]);
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
                if(graph[i][j]=='Y'){
                    sx = i;
                    sy = j;
                }
        memset(vis,false,sizeof(vis));
        bfs();
        for(int i=0;i<n;i++)
            printf("%s\n",graph[i]);
        printf("\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值