Note that there are some explanatory texts on larger screens.

plurals
  1. POPython, recursive breadth first search
    primarykey
    data
    text
    <p>I am trying to find the shortest path on a grid(list of lists of tuples) I've gotten this so far:</p> <pre><code>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) </code></pre> <p>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?</p> <p>I was thinking this: </p> <pre><code>for neighbor in neighbors: next = pathFinder(neighbor) if next: visited.append(cure) return curr else: return None </code></pre> <p>but, correct me if I'm wrong, but that would result in depth first search, not breadth.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload