走迷宫(BFS深搜)

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

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

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

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

输入格式

第一行包含两个整数 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

 对于迷宫的最短路问题,这是最经典的bfs问题,用来解决最短路问题,bfs要用到队列先进先出的思想,一层一层遍历,因此当遍历到终点时,此时所在层数最少,路径最短。

代码实现如下:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
typedef pair<int,int> PII;
int n,m;
const int N=110;
int arr[N][N],d[N][N];//d[][]表示该点离起点的距离,走了多少步
int bfs()
{
    queue<PII> q;//定义一个队列,队列先进先出
    q.push({0,0});
    memset(d,-1,sizeof(d));//把d数组初始化为-1,为0则表示已经走过
    d[0][0]=0;
    while(q.size())//假如队列不为空
    {
        PII t=q.front();//用一个pair取队列的第一个坐标
        q.pop();//清空第一个队列坐标
        for(int i=0;i<4;i++)//依次循环判断哪一步能走,能走则放入队列中
        {
            int x=t.first+dx[i],y=t.second+dy[i];//由于每次x和y都是重新定义的,因此不会受到两步都能走成立的影响
            if(x>=0&&x<n&&y>=0&&y<m&&d[x][y]==-1&&arr[x][y]==0)//假如在界内并且未走过且不撞墙
            {
                d[x][y]=d[t.first][t.second]+1;
                q.push({x,y});//入队
            }
        }
    }
    return d[n-1][m-1];
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++) cin>>arr[i][j];
        

    cout<<bfs()<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中使用BFS算法解决走迷宫题的示例代码: ```java import java.util.*; public class MazeBFS { static int[][] maze = {{0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 1, 1, 1, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0}, {0, 1, 1, 1, 0, 1, 1, 0}, {0, 1, 0, 0, 0, 0, 1, 0}, {0, 1, 1, 1, 1, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0}}; static int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; public static void main(String[] args) { int startX = 1; int startY = 1; int endX = 5; int endY = 6; Queue<Node> queue = new LinkedList<>(); boolean[][] visited = new boolean[maze.length][maze[0].length]; Node startNode = new Node(startX, startY, 0); queue.offer(startNode); visited[startX][startY] = true; while (!queue.isEmpty()) { Node node = queue.poll(); int x = node.x; int y = node.y; int step = node.step; if (x == endX && y == endY) { System.out.println(step); break; } for (int i = 0; i < 4; i++) { int nextX = x + direction[i][0]; int nextY = y + direction[i][1]; if (nextX >= 0 && nextX < maze.length && nextY >= 0 && nextY < maze[0].length && maze[nextX][nextY] == 0 && !visited[nextX][nextY]) { queue.offer(new Node(nextX, nextY, step + 1)); visited[nextX][nextY] = true; } } } } static class Node { int x; int y; int step; public Node(int x, int y, int step) { this.x = x; this.y = y; this.step = step; } } } ``` 在上述代码中,我们使用了一个Node类来表示迷宫中的每个节点,其中包含了x和y坐标以及当前步数step。我们使用一个队列来进行BFS搜索,并且使用一个二维布尔数组visited来记录每个节点是否已经被访过。 我们从起点开始,将其加入队列中,并将其visited值设为true。然后不断从队列中取出节点进行搜索,直到队列为空或者找到终点为止。对于每个节点,我们尝试向四个方向扩展,如果扩展出来的节点在迷宫范围内、不是墙、没有被访过,就将其加入队列中,并将visited值设为true。 最终,如果搜索到了终点,就输出步数并停止搜索。如果队列为空仍然没有找到终点,说明终点不可达。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值