Implementing a Cost-Aware Dijkstra's Algorithm

Learn how to implement a cost-effective Dijkstra's algorithm on the mazes using Ruby.

The WeightedCell class

In this lesson, we're going to apply Dijkstra's algorithm to the mazes by creating a few mazes with some lava pits. To make this work in code, we need two things: a cell that can be assigned a weight and a grid composed of these weighted cells.

The cell is straightforward. We’re just going to subclass Cell and add weight information to it. Also, since our Dijkstra’s algorithm was implemented on Cell, we put our updated implementation in the subclass, too, by overriding the original distances method.

Get hands-on with 1200+ tech skills courses.