Block the Spreading Fire

2022

Contents
Problem
A fire spreads across a grid of trees and houses. I can cut one firebreak at a time, each cut takes several turns, and the score is the value that doesn't burn.
Hard limit
About 95 ms of search per turn after a longer first turn. The number of possible cut orders is far too large to check them all.
Approach
Search for the region of the map worth saving, not for a list of cuts. The cuts and their order come from the region's boundary. Hill climbing finds a first region, then simulated annealing (a search that sometimes accepts a worse option, less often as time runs out, so it doesn't get stuck) improves it.
Result
25th of 855 on the puzzle's leaderboard. When I wrote up the approach on the CodinGame forum in 2022 it was ranked 4th.
What it shows
How the problem is represented matters more than the search built on top of it. A random list of cuts almost never scores, so the search would spend its time on nothing.
In the replay
The white outline marks the region the cuts end up protecting, so you can see where the plan is heading from the first turn. Turn it off to watch only the fire and the cuts.

Replays

Below are recorded runs of my solution on six of the puzzle’s test cases. The page replays the moves my program printed; it doesn’t run any search. The fire is simulated in the browser from the puzzle rules, and the final score it reaches matches the one CodinGame reported for each run. The program doesn’t print the region it chose, so the outline is worked out from the end of the simulation: the cells that are still untouched once the fire is out. That is the region the printed cuts protect.

Keyboard: focus the grid, then use the left and right arrows to step, space to play or pause, and Home or End to jump to the start or finish. The animation starts by itself unless your system asks for reduced motion.

The problem in full

A fire starts on a grid of trees and houses and spreads to the four neighbouring cells each time a burning cell finishes burning. Each turn you can name one cell to cut, which makes it safe at once, but cutting takes several turns and you can’t cut anything else until it’s done. The score is the value of the cells that neither burned nor got cut, so the job is to decide which firebreak to build, and in what order, before the fire gets there.

How my solution works

This is my explanation from the CodinGame forum, posted in July 2022 when the solution was ranked 4th, with light edits.

The most significant part of my algorithm is how I represent a candidate solution. Representing candidate solutions as a sequence of random cuts is very unlikely to yield any points, or at least to make effective use of the cuts. All sequences of cuts that generate some points have something in common: they disconnect sections of the map from the fire.

Therefore I represent my candidate solutions as a set of cells on the map that I want to save (be disconnected from where the fire is). From this I can determine a few things:

I initially used a simple hill climbing approach, as follows:

This approach was already quite effective. Regarding performance, I could get around 60-80k iterations (of the inner loop) for the 7th test case.

The improvements that got my solution to 4th:

Back to CodinGame and the other post-mortems.

Back to all projects Get in touch