对网格有约束的迷宫

时间:2014-10-13 21:13:46

标签: python-3.x path-finding maze

在网上有一些算法或解决一个简单的迷宫;但我想解决的问题有点复杂。 这是一个例子:

####################
#S #D #           #
#  # ##  ##  ###D###
#     #   #   #    #
## #  #   #     ## #
#     ### #####    #
# #   #  D#  #   ###
# ### ### ## # #####
#   #     #       E#
####################

S代表StartPoint; E代表EndPoint。 D(s)是收集点。我想找到从S到E的所有路径经过D.然后我想区分最短的路径。

以下是A *算法的示例,我如何确保路径通过D点。

def aStar(self, graph, current, end):
    openList = []
    closedList = []
    path = []

    def retracePath(c):
        path.insert(0,c)
        if c.parent == None:
            return
        retracePath(c.parent)

    openList.append(current)
    while len(openList) is not 0:
        current = min(openList, key=lambda inst:inst.H)
        if current == end:
            return retracePath(current)
        openList.remove(current)
        closedList.append(current)
        for tile in graph[current]:
            if tile not in closedList:
                tile.H = (abs(end.x-tile.x)+abs(end.y-tile.y))*10 
                if tile not in openList:
                    openList.append(tile)
                tile.parent = current
    return path

我的问题是我如何修改上面的搜索算法,以便它通过所有D点。我希望所有路径都包括D点。

0 个答案:

没有答案