迷宫问题(bfs从入门到应用)

给定一个 n×n 的二维数组,如下所示:

int maze[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,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

数据保证至少存在一条从左上角走到右下角的路径。

输入格式

第一行包含整数 n。

接下来 n 行,每行包含 n 个整数 0 或 1,表示迷宫。

输出格式

输出从左上角到右下角的最短路线,如果答案不唯一,输出任意一条路径均可。

按顺序,每行输出一个路径中经过的单元格的坐标,左上角坐标为 (0,0),右下角坐标为 (n−1,n−1)。

数据范围

0≤n≤1000

输入样例:
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
输出样例:
0 0
1 0
2 0
2 1
2 2
2 3
2 4
3 4
4 4

这个问题是需要你输出一个最短路的完整路径,那么我们先简化一下这个问题,如果只是让你求最短路径的话,那么是有非常非常多的办法dfs、bfs、dijkstra等等(dfs和bfs可以看看Acwing 1116. 马走日(dfs模板)-CSDN博客洛谷P1783 数独(c++lambda函数广搜详解)_数独 洛谷-CSDN博客) 

那这里我们介绍一下bfs的方法,我们用bfs来遍历搜索整张图,最后dist[n][n]就是我们所求的最短路径

代码:

#include <iostream>
#include <cstring>
#define x first
#define y second
using namespace std;

typedef pair<int,int> PII;

const int N = 1e3 + 10;
int w[N][N],dist[N][N];
PII q[N];
int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int n;

void bfs(int u,int v){
    memset(dist,-1,sizeof(dist));
    PII p = {u,v};
    dist[u][v] = 0;
    q[0] = {u,v};
    int tt = 0 ,hh = 0;
    while(hh <= tt){
        PII t = q[hh ++];
        for(int i = 0;i < 4;i ++){
            int nx = t.x + d[i][0],ny = t.y + d[i][1];
            if(nx < 1 || nx > n || ny < 1 || ny > n || w[nx][ny]) continue;
            if(dist[nx][ny] != -1) continue;
            dist[nx][ny] = dist[t.x][t.y] + 1;
            q[++ tt] = {nx,ny};
        }
    }
}

int main()
{
    cin >> n;
    for(int i = 1;i <= n;i ++){
        for(int j = 1;j <= n;j ++){
            cin >> w[i][j];
        }
    }
    bfs(1,1);
    cout << dist[n][n] << endl;
    return 0;
}

那怎么输出这些路径的点呢?其实不难,我们逆向想,假如给了我们一个最短路径,那么我们把这个最短路径从终点开始,记录每个点的前驱,然后反着输出就可以了

代码:

#include <cstring>
#include <iostream>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 1010, M = N * N;

int n;
int g[N][N];
PII q[M];
PII pre[N][N];

void bfs(int sx, int sy)
{
    int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

    int hh = 0, tt = 0;
    q[0] = {sx, sy};

    memset(pre, -1, sizeof pre);
    pre[sx][sy] = {0, 0};
    while (hh <= tt)
    {
        PII t = q[hh ++ ];

        for (int i = 0; i < 4; i ++ )
        {
            int a = t.x + dx[i], b = t.y + dy[i];
            if (a < 0 || a >= n || b < 0 || b >= n) continue;
            if (g[a][b]) continue;
            if (pre[a][b].x != -1) continue;

            q[ ++ tt] = {a, b};
            pre[a][b] = t;
        }
    }
}

int main()
{
    scanf("%d", &n);

    for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
            scanf("%d", &g[i][j]);

    bfs(n - 1, n - 1);

    PII end(0, 0);

    while (true)
    {
        printf("%d %d\n", end.x, end.y);
        if (end.x == n - 1 && end.y == n - 1) break;
        end = pre[end.x][end.y];
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值