Implementing Dijkstra’s
To bring this simplified version of Dijkstra’s together, we’re going to lean on a new class, called Distances, which will keep track of how far each cell is from the reference cell (the cell where we start counting from). Once we’ve implemented that class, we’ll add our actual implementation of Dijkstra’s to the Cell class, which will let us apply it pretty much anywhere we need to.
So, first off, let’s add that Distances class. For now, it’s just a simple wrapper around a Hash instance, but we’ll be making it more useful soon. Create a new file named distances.rb, and add the following to it.
distances.rb | |
| class Distances |
| def initialize(root) |
| @root = root |
| @cells = {} |
| @cells[@root] = 0 |
| end |
| |
| def [](cell) ... |
Get Mazes for Programmers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.