Search⌘ K

Finding the Shortest Path

Explore how to implement Dijkstra's algorithm to find the shortest path between points in a maze. Understand how to work backward from a goal cell to trace the optimal path, enhance the Distances class with a path_to method, and use visualization techniques to display the solution path clearly. This lesson equips you with practical skills to solve mazes programmatically and visualize maze paths effectively.

Shortest path in our maze

The whole point of this exercise is to help us find a solution to our maze, a path between any two arbitrary points, so let’s tackle that now. We’ll implement more or less what we described previously, walking the path backward from our goal, and looking for neighboring cells with sequentially smaller distances.

We first decide where we want our path to end (let’s make it the southwest corner), and work backward from there. For each cell along the path, the neighboring cell with the lowest distance will be the next step of the solution.

Let's make the Distance class more useful. In the ...