2

There are some algorithm or solving a simple maze on the web; but what I am trying to solve is a bit more complicated. Here is an example:

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

S represents the StartPoint; E represents the EndPoint. D(s) are the collecting point. I want to find the all the paths from S to E which go through D. Then I want to distinguish the shortest path.

Here is an example of A* algorithm, How can I assure that paths go through D points.

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

My question is that How i can modify above search algorithm so that it passes through all D points. I want all the paths included D points.

FrankW
  • 6,609
  • 4
  • 27
  • 42
H'H
  • 121
  • 1

0 Answers0