Implementing the Hunt-and-Kill algorithm

Learn to implement the Hunt-and-Kill algorithm in Ruby.

The HuntAndKill class

There really aren’t any surprises in the implementation here. As expected, we start by choosing a cell at random and then doing our random walk. In that respect, it looks a lot like our implementation of the Aldous-Broder algorithm. The similarity ends, though, when we discover we’re at a dead end and there are no more unvisited neighbor cells. That triggers the hunt phase, where we loop over the grid, looking for unvisited cells with visited neighbors. Let's look at the code.

Get hands-on with 1400+ tech skills courses.