算法提高 学霸的迷宫

本文介绍了一种使用广度优先搜索(BFS)算法解决二维迷宫最短路径问题的方法。该算法可以找到从迷宫起点到终点的最短路径,并确保路径的字典序最小。文章提供了完整的C++实现代码,适用于不同大小的迷宫。

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

问题描述
  学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗。但学霸为了不要别人打扰,住在一个城堡里,城堡外面是一个二维的格子迷宫,要进城堡必须得先通过迷宫。因为班长还有妹子要陪,磨刀不误砍柴功,他为了节约时间,从线人那里搞到了迷宫的地图,准备提前计算最短的路线。可是他现在正向妹子解释这件事情,于是就委托你帮他找一条最短的路线。
输入格式
  第一行两个整数n, m,为迷宫的长宽。
  接下来n行,每行m个数,数之间没有间隔,为0或1中的一个。0表示这个格子可以通过,1表示不可以。假设你现在已经在迷宫坐标(1,1)的地方,即左上角,迷宫的出口在(n,m)。每次移动时只能向上下左右4个方向移动到另外一个可以通过的格子里,每次移动算一步。数据保证(1,1),(n,m)可以通过。
输出格式
  第一行一个数为需要的最少步数K。
  第二行K个字符,每个字符∈{U,D,L,R},分别表示上下左右。如果有多条长度相同的最短路径,选择在此表示方法下字典序最小的一个。
样例输入
Input Sample 1:
3 3
001
100
110

Input Sample 2:
3 3
000
000
000
样例输出
Output Sample 1:
4
RDRD

Output Sample 2:
4
DDRR
数据规模和约定
  有20%的数据满足:1<=n,m<=10
  有50%的数据满足:1<=n,m<=50
  有100%的数据满足:1<=n,m<=500。


经典bfs

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#include <map>
#include <math.h>
#include <stack>
#include <queue>
#define Max 510
#define inf 100000000
using namespace std;
int vis[Max][Max],r,l;
int Mov[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
char str[5] = "UDLR",m[Max][Max],step[Max];
struct Node{
    int x;
    int y;
    int s;
    char sp;
}path[Max][Max];
int BFS(){
    Node now;
    memset(vis, 0, sizeof(vis));
    now.x = now.y =0;
    path[now.x][now.y].s = 0;
    path[now.x][now.y].sp = 0;
    queue<Node>q;
    q.push(now);
    while (q.size()>0) {
        now = q.front();
        if (now.x == r-1 && now.y == l-1) {
            break;
        }
        q.pop();
        Node next = now;
        //printf("now:x %d y %d  step %d  straight %c\n",now.x,now.y,path[now.x][now.y].s,path[now.x][now.y].sp);
        for (int i=0; i<4; i++) {
            next.x = now.x+Mov[i][0];
            next.y = now.y+Mov[i][1];
            if (!vis[next.x][next.y]&&(next.x>=0&&next.x<r&&next.y>=0&&next.y<l)&& m[next.x][next.y] != '1' ) {
                vis[next.x][next.y] = 1;
                path[next.x][next.y] = now;
                path[next.x][next.y].sp = str[i];
                path[next.x][next.y].s = path[now.x][now.y].s+1;
                q.push(next);
               // printf("next:x %d y %d  step %d  straight %c\n",next.x,next.y,path[next.x][next.y].s,path[next.x][next.y].sp);
            }
        }
    }
    //printf("%d\n",path[r-1][l-1].s);
    return path[r-1][l-1].s;
}
int main(){
    while (scanf("%d%d",&r,&l)!=EOF) {
        for (int i=0; i<r; i++) {
                scanf("%s",m[i]);
        }
        printf("%d\n",BFS());
        int len = 0;
        int nx = r-1,ny = l-1;
        int nextx = nx,nexty = ny;
        while (1) {
            nx = nextx;
            ny = nexty;
            if (nx + ny == 0) {
                break;
            }
            step[len++] = path[nx][ny].sp;
            nextx = path[nx][ny].x;
            nexty = path[nx][ny].y;
        }
        for (int i=len-1; i>=0; i--) {
            printf("%c",step[i]);
        }
        printf("\n");
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值