数据结构——python解决迷宫问题(深度优先和广度优先)

文章目录

迷宫问题

栈解决------深度优先------回溯法

python 复制代码
""""""
"""
规定每次按照某种的方式前进
每次前进都将节点的位置存入栈,这样栈中就存放着走过的路线
当不能再继续前进时:后退出栈,直到可以用另一条路可以前进
"""

maze = [
    [0, 1, 0, 0, 0],
    [0, 1, 0, 1, 0],
    [0, 0, 0, 1, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0]
]

# 前进方式
dirs = [
    lambda x, y: (x - 1, y),
    lambda x, y: (x, y + 1),
    lambda x, y: (x + 1, y),
    lambda x, y: (x, y - 1),
]


def solve(x1, y1, x2, y2):
    stack = []
    stack.append((x1, y1))
    while len(stack):
        current_node = stack[-1]
        if current_node[0] == x2 and current_node[1] == y2:
            for i in stack:
                print(i)
            return True

        for dir in dirs:
            next_node = dir(*current_node)
            if 0 <= next_node[0] < len(maze) and 0 <= next_node[1] < len(maze[0]):
                if maze[next_node[0]][next_node[1]] == 0:
                    stack.append(next_node)
                    maze[next_node[0]][next_node[1]] = 2
                    break
        else:
            maze[next_node[0]][next_node[1]] = 2
            stack.pop()
    else:
        print('没有通路')
        return False


solve(0, 0, 4, 4)

队列解决------广度优先

python 复制代码
""""""
"""
思想:
多条路径同时前进,第一个到达的就是最短路径
通过记录每个节点的上一个节点的位置实现
实现:
一个列表记录此时到达的全部位置(多条路线同时进行就有多个位置),再一个列表所有已经到过的节点位置以及每个节点的上一个位置
"""

from collections import deque

maze = [
    [0, 1, 0, 0, 0],
    [0, 1, 0, 1, 0],
    [0, 0, 0, 1, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0]
]

# 前进方式
dirs = [
    lambda x, y: (x - 1, y),
    lambda x, y: (x, y + 1),
    lambda x, y: (x + 1, y),
    lambda x, y: (x, y - 1),
]


def print_r(path):
    real_path = []
    real_item_index = len(path) - 1  # real_item:通路上的元素(最开始一定是path的最后一个元素)
    while real_item_index >= 0:
        real_path.append(path[real_item_index][0:2])
        real_item_index = path[real_item_index][2]
    real_path.reverse()
    for node in real_path:
        print(node)


def solve(x1, y1, x2, y2):
    q = deque()
    path = []
    q.append((x1, y1, -1))
    while len(q) > 0:
        current_node = q.popleft()
        path.append(current_node)

        if current_node[0] == x2 and current_node[1] == y2:
            # 到达终点,通过path逐层向前找到完整路径
            print_r(path)
            return True

        for dir in dirs:
            next_node = dir(current_node[0], current_node[1])
            if 0 <= next_node[0] < len(maze) and 0 <= next_node[1] < len(maze[0]):
                if maze[next_node[0]][next_node[1]] == 0:
                    q.append((next_node[0], next_node[1], len(path) - 1))
                    maze[next_node[0]][next_node[1]] = 2
    else:
        print('未找到通路')
        return False


solve(0, 0, 4, 4)

若有错误与不足请指出,关注DPT一起进步吧!!!

相关推荐
计算机源码社6 小时前
【大数据项目实战】基于大数据的影视内容生态综合质量分析与可视化-基于数据挖掘的影视内容类型共现与口碑聚类分析系统
大数据·人工智能·python·数据挖掘·数据分析·毕业设计·课程设计
C^h7 小时前
pytorch 适合初学者 0基础学习
人工智能·pytorch·python
小柯南敲键盘7 小时前
跨马翻译:AI批量图片翻译工具,跨境电商视频字幕翻译与智能抠图一体搞定
人工智能·python·音视频
2601_962297257 小时前
Python里behave和pytest-bdd哪个更适合中大型项目?为什么?
python·bdd·行为驱动开发·behave·pytest-bdd
2601_967760787 小时前
2026年PDF压缩与页码添加工具技术实测:性能、算法与本地化适配深度对比
算法·pdf
不会就选b8 小时前
算法日常・每日刷题--<贪心>7
数据结构·算法·leetcode
moonrailgun8 小时前
用 Node.js 复刻 Codex Astra 的终端星光
前端·javascript·算法
罗西的思考9 小时前
[Agent Memory / 强化学习] MemPO源码学习笔记 ---(1)--- 总体
人工智能·算法·机器学习
YCOSA20259 小时前
系统工具使用检测.exe (仅供娱乐)
python·microsoft
2601_962295339 小时前
如何python实现网页的自动化
python·selenium·beautifulsoup·requests·网页自动化