I am trying to find the shortest path on a grid(list of lists of tuples) I've gotten this so far:
def planPath(self, x, y, goal, board):
visited = []
path = []
def pathFinder(curr):
visited.append(curr)
neighbors = None
if curr == goal:
visited.append(curr)
return curr
neighbors = [neighbor for neighbor in curr.neighbors if type(neighbor) is not Land.Water and neighbor not in visited]
neighbors.sort(key=lambda neighbor: abs(neighbor.location[0] - goal.location[0]) + abs(neighbor.location[1] - goal.location[1]) + abs(neighbor.elevation - goal.elevation), reverse=False)
x, y are the current location, is obvious, and board is, well the entire board. The thought was that it would be recursive. For every call I would get the neighbors for the current node, filter out the water(untravelable) and the visited. Then sort by the closest to the goal. Next I want to go through and do a breadth first search. So I will expand all the neighbors, then expand each of their neighbors and so on. Each branch would go on until they have no neighbors, they would return None and be done with. Then end result being that the only one to return will be the first brach to reach the goal, i.e. the shortest. Does anyone know how I could do this?
I was thinking this:
for neighbor in neighbors:
next = pathFinder(neighbor)
if next:
visited.append(cure)
return curr
else: return None
but, correct me if I'm wrong, but that would result in depth first search, not breadth.