

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Traffic Tiles
incomplete
2: Traffic Grid
incomplete
3: A* Search Algorithm
incomplete
4: A* Code
incomplete
5: A* Search Review
incomplete
This lesson's interactive features are locked, please to keep using them
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.
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.