寻找迷宫中的最短路径

1.题目描述

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

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

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

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

输入格式

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

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

输出格式

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

数据范围

1≤n,m≤1001≤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

题目来源:https://www.acwing.com/problem/content/846/

2.解题思路

1,起始点坐标加入队列,设置距离为0

2,while 队列不为空,队头出队一个节点,如果这个节点的上/下/左/右可以到达(不碰壁、不超过边界,之前未被访问),则把能到达的加入队列,距离+1

3,如果到达终点,返回距离

3.代码实现

import sys
n, m = map(int, sys.stdin.readline().strip().split())
A = []
for i in range(n):
    A.append(list(map(int, sys.stdin.readline().strip().split())))
# visited初始化
vis = [[False for j in range(m)] for i in range(n)]
vis[0][0] = True
que = []
# 加入的是这个节点的元组表示(x,y,距离)
que.append((0, 0, 0))
# 四个方向
dx=[-1,0,1,0]
dy=[0,-1,0,1]
while len(que) != 0:
    x, y, c = que.pop(0)
    # 出口,到达终点
    if x == n-1 and y == m-1:
        print c
    for i in range(4):
        nx=x+dx[i]
        ny=y+dy[i]
        if nx >= 0 and nx < n and ny >= 0 and ny < m and A[nx][ny] == 0 and vis[nx][ny]==False:
            que.append((nx, ny, c + 1))
            vis[nx][ny] = True
    

 

寻找迷宫最短路径可以使用广度优先搜索(BFS)算法来实现。以下是一个简单的C++代码示例: ```c++ #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 100; // 最大迷宫大小 const int dx[] = {0, 0, 1, -1}; // 上下左右四个方向移动的步长 const int dy[] = {1, -1, 0, 0}; int maze[MAXN][MAXN]; // 迷宫地图 int dist[MAXN][MAXN]; // 记录每个位置到起点的最短距离 bool vis[MAXN][MAXN]; // 标记每个位置是否已访问 int n, m; // 迷宫大小 struct Node { int x, y; // 当前位置 int step; // 走到当前位置的步数 }; bool check(int x, int y) { // 判断当前位置是否越界或障碍物 return x >= 0 && x < n && y >= 0 && y < m && maze[x][y] != 1; } int bfs(int sx, int sy, int ex, int ey) { // 搜索起点到终点的最短距离 queue<Node> q; memset(dist, -1, sizeof(dist)); // 初始化距离为-1 memset(vis, false, sizeof(vis)); // 初始化未访问 Node start = {sx, sy, 0}; q.push(start); vis[sx][sy] = true; dist[sx][sy] = 0; while (!q.empty()) { Node cur = q.front(); q.pop(); if (cur.x == ex && cur.y == ey) { // 到达终点 return cur.step; // 返回最短距离 } for (int i = 0; i < 4; i++) { // 四个方向扩展 int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (check(nx, ny) && !vis[nx][ny]) { // 如果可以走且未访问过 Node next = {nx, ny, cur.step + 1}; q.push(next); vis[nx][ny] = true; dist[nx][ny] = cur.step + 1; } } } return -1; // 无法到达终点 } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> maze[i][j]; } } int sx, sy, ex, ey; // 起点和终点坐标 cin >> sx >> sy >> ex >> ey; int shortest = bfs(sx, sy, ex, ey); if (shortest == -1) { cout << "无法到达终点" << endl; } else { cout << "最短距离为:" << shortest << endl; } return 0; } ``` 在输入迷宫地图和起点终点坐标后,调用`bfs`函数即可返回起点到终点的最短距离。如果无法到达终点,函数返回-1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值