在python中将深度优先转换为广度优先

2024-09-28 01:25:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于迷宫探路者的问题。我有一个深度优先的解决方案,但我想要一个广度优先的解决方案。 这是深度优先:

def find_path(pathList):
    if pathList[-1] == goal:
        return pathList

    for i in adjacent_passages(labyrinth, pathList[-1]):
        if i not in pathList:
            found_path = find_path(pathList + [i])
            if found_path:
                return found_path

这就是我在广度优先问题上取得的“进展”:

^{pr2}$

迷宫是迷宫的矩阵,相邻的通道找到相邻的正方形:)


Tags: pathinforreturnifdeffind解决方案

热门问题