简单二维迷宫bfs最短路径输出

这篇博客介绍了如何解决一个二维数组迷宫问题,使用宽度优先搜索(BFS)算法寻找从左上角到右下角的最短路径。博主给出了详细的过程和代码实现,并提到该方法也可应用于求解任意两点之间的最短距离。在代码中,还实现了路径的回溯输出功能。
摘要由CSDN通过智能技术生成

给定一个 n × m 的二维整数数组,用来表示一个迷宫,数组中只包含 0 或 1 ,其中 0 表示可以走的路,1 表示不可通过的墙壁。

最初,有一个人位于左上角 (1, 1) 处,已知该人每次可以向上、下、左、右任意一个方向移动一个位置。

请问,该人从左上角移动至右下角 ( n, m ) 处,至少需要移动多少次。

数据保证 (1, 1) 处和 ( n, m ) 处的数字为 0 ,且一定至少存在一条通路。

Input

第一行包含两个整数 nm 。1 ≤ n, m ≤ 100

接下来 n 行,每行包含 m 个整数( 0 或 1 ),表示完整的二维数组迷宫。

Output

输出一个整数,表示从左上角移动至右下角的最少移动次数以及移动路径。

Sample Input

5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Output

8

1 1
2 1
3 1
3 2
3 3
3 4
3 5
4 5
5 5

蛛丝:就是在普通的bfs遍历二维迷宫的时候新开一个数组记录该点是经过了哪个变化而来,有4个变化,例如样例里面坐标(2,1)的点也就是d[2][1]的点是d[1][1]经过dx[2],dy[2]得到,就将2作为st[2][1]的值输入,最后遍历到终点时根据st数组的值回溯到d[1][1],将路上的节点坐标存入一个vector数组后倒序输出就行了,就是从起点到终点的路上的坐标的点了

如果是换成求任意两点之间的距离也很简单,改一下起点终点的值就差不多了,代码变动不大。

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <stack>
using namespace std;
const int N=1010;
typedef pair<int ,int>PII;
 int s1,s2,t1,t2;
int st[N][N];
int d[N][N];
int p[N][N];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int dist[N][N];
int n,m;
int bfs()
{
    memset(dist,-1,sizeof dist);
    memset(st,-1,sizeof st);
    queue<PII>que;
    que.push({1,1});
    st[1][1]=-1;
    dist[1][1]=0;
    while(que.size())
    {
        auto t=que.front();
        que.pop();
        for(int i=0;i<4;i++)
        {
            int x=t.first+dx[i],y=t.second+dy[i];
            if(x>=1&&x<=n&&y>=1&&y<=m&&dist[x][y]==-1&&d[x][y]==0)
            {
                st[x][y]=i;//记录该点是经过哪个操作得到的
                dist[x][y]=dist[t.first][t.second]+1;
                que.push({x,y});
            }
        }
    }
    return dist[n][n];
}
void ch(int x,int y) //输出路径
{
    vector<PII>v;
    while(st[x][y]!=-1)
    {
        int a=x,b=y;
        v.push_back({x,y});
        x=x-dx[st[a][b]];
        y=y-dy[st[a][b]];
    }
    v.push_back({1,1});
    for(int i=v.size()-1;i>=0;i--)
        cout<<v[i].first<<' '<<v[i].second<<endl;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&d[i][j]);
        }
    }
    cout<<bfs()<<endl;

   ch(n,n);
return 0;
}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于C++的BFS二维迷宫最短路径的示例代码: ```cpp #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 1005; // 最大节点数 const int INF = 0x3f3f3f3f; // 无穷大 int n, m; // n表示迷宫的行数,m表示迷宫的列数 int sx, sy, ex, ey; // 起点和终点的坐标 int maze[MAXN][MAXN]; // 存储迷宫的地图 int dis[MAXN][MAXN]; // 存储起点到每个节点的距离 bool vis[MAXN][MAXN]; // 记录每个节点是否被访问过 struct node { int x, y; // 节点坐标 }; void bfs() { memset(dis, INF, sizeof(dis)); // 初始化距离为无穷大 memset(vis, false, sizeof(vis)); // 初始化所有节点都未被访问过 dis[sx][sy] = 0; // 起点到自己的距离为0 vis[sx][sy] = true; // 起点已经访问过了 queue<node> q; // 定义一个队列,用于广度优先搜索 q.push({sx, sy}); // 将起点加入队列 while (!q.empty()) { node u = q.front(); q.pop(); // 取出队首节点 int dx[4] = {0, 0, 1, -1}; // 四个方向 int dy[4] = {1, -1, 0, 0}; for (int i = 0; i < 4; ++i) { int x = u.x + dx[i]; int y = u.y + dy[i]; if (x >= 1 && x <= n && y >= 1 && y <= m && maze[x][y] != 1 && !vis[x][y]) { // 如果这个点可行且未被访问过 dis[x][y] = dis[u.x][u.y] + 1; // 更新距离 vis[x][y] = true; // 标记为已访问 q.push({x, y}); // 将其加入队列 } } } } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> maze[i][j]; } } cin >> sx >> sy >> ex >> ey; bfs(); cout << "起点到终点的最短距离为:" << dis[ex][ey] << endl; return 0; } ``` 这段代码使用二维数组存储迷宫地图和节点距离,时间复杂度为O(nm),其中n表示迷宫的行数,m表示迷宫的列数。需要注意的是,在实际使用中,可能需要根据具体情况调整代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值