[C++][算法基础]走迷宫(BFS)

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

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

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

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

输入格式

第一行包含两个整数 n 和 m。

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

输出格式

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

数据范围

1≤n,m≤100

输入样例:
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
输出样例:
8

代码:

#include<iostream>
#include<queue>
using namespace std;

const int N = 110;

int n,m;
int G[N][N];
int dist[N][N];
queue<pair<int,int>> Q;

int bfs(){
    int head = 0,tail = 0;
    int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
    dist[0][0] = 0;
    Q.push({0,0});
    while(Q.size()!=0){
        auto now = Q.front();
        Q.pop();
        for(int i = 0;i < 4;i++){
            int x = now.first + dx[i];
            int y = now.second + dy[i];
            if(x >= 0 && x < n && y >= 0 && y < m && G[x][y] == 0 &&dist[x][y] == -1){
                dist[x][y] = dist[now.first][now.second] + 1;
                Q.push({x,y});
            }
        }
    }
    return dist[n-1][m-1];
}

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

 

  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言实现广度优先算法走迷宫的示例代码: ``` #include <stdio.h> #include <stdlib.h> #define MAX_ROW 5 #define MAX_COL 5 struct point { int row, col; } queue[512]; int maze[MAX_ROW][MAX_COL] = { 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, }; int visited[MAX_ROW][MAX_COL]; int front = 0, rear = 0; void enqueue(struct point p) { queue[rear++] = p; } struct point dequeue(void) { return queue[front++]; } int is_empty(void) { return front == rear; } void visit(int row, int col, struct point pre) { struct point visit_point = { row, col }; maze[row][col] = 2; visited[row][col] = 1; printf("(%d, %d) ", row, col); if (pre.row != -1) printf("(%d, %d) ", pre.row, pre.col); enqueue(visit_point); } void bfs(int row, int col) { struct point p = { row, col }; enqueue(p); while (!is_empty()) { struct point current = dequeue(); if (maze[current.row][current.col] == 2) continue; if (current.row == MAX_ROW - 1 && current.col == MAX_COL - 1) { printf("(%d, %d)\n", current.row, current.col); printf("Congratulations, you have solved the maze!\n"); return; } visit(current.row, current.col - 1, current); visit(current.row - 1, current.col, current); visit(current.row, current.col + 1, current); visit(current.row + 1, current.col, current); } printf("Sorry, there is no path to the exit.\n"); } int main(void) { bfs(0, 0); return 0; } ``` 代码中,我们首先定义了一个5*5的迷宫,其中0表示可通行的路,1表示障碍物,2表示已经访问过的点。然后定义了一个point结构体作为队列中的元素,同时也定义了一个队列数组queue,用于存储待访问的点。visited数组用于记录哪些点已经被访问过。 enqueue函数用于将一个点加入队列中,dequeue函数用于从队列中取出一个点。is_empty函数用于判断队列是否为空。 visit函数用于访问一个点,将其标记为已访问,并将其加入队列中。bfs函数用于实现广度优先搜索,从起点开始遍历迷宫,遇到障碍物或已经访问过的点则跳过,否则访问该点,并将其相邻的四个点加入队列中。如果找到了终点,则输出路径并结束程序。如果队列为空仍未找到终点,则输出无法到达终点的提示信息。 在主函数中,我们调用bfs函数并传入起点坐标(0,0)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值