We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

A* Code

A* search is very similar to Dijkstra's algorithm, except we add in a heuristic based on Manhattan distance, a.k.a. "taxicab distance." Our implementation of A* will, however, look quite a bit different from the Dijkstra's algorithm we wrote because we'll be using a priority queue this time.

Let's build our most effective route detection algorithm yet for Mappy! We'll be using the A* algorithm to efficiently find the shortest path through the grid, taking into account traffic jams.

Assignment

  1. next and dest are Tile objects. Our heuristic will be the distance between the node we're considering moving to (next) and the destination. Return the result of the distance formula:

    abs(next.x - dest.x) + abs(next.y - dest.y)
    

    You'll notice the type hints use quotes around class names like "Tile" and "TrafficGrid". These are forward references – they let us reference classes that are defined later in the file.