Editorial for WC '16 Contest 3 S3 - Puzzle Rooms
Submitting an official solution before solving the problem yourself is a bannable offence.
This is the sort of problem which generally can't be solved efficiently, but given its structure, it should be a safe bet that some pattern to the answers will emerge for relatively small values of , and extend to arbitrarily large values of . This proves to indeed be the case, and the pattern can be visually seen fairly easily if all of the answers up to around are found (though guessing the pattern earlier, around , is also reasonable).
For such small values of , a brute force algorithm will suffice, though not just any one. The most natural approach is to try all possible sets of walls on the grid, and for each one, find the furthest-away pair of empty cells using BFS in another time. This process is incredibly inefficient, but with a few optimizations, it can be made to successfully yield the answers up around in a reasonable amount of time – most likely not within the problem's time limit, but at least fast enough to run at home for a while, look at the answers, determine their pattern, and put together a solution with hardcoding.
A more elegant brute force algorithm exists which is both simpler, and can also produce the required answers within a few seconds. We can instead consider directly generating the path from the starting cell to the destination cell. We'll start from each possible starting cell, and repeatedly recurse to adjacent cells, while maintaining the cells which the path has already visited. The only requirement is that the path never visits a cell adjacent to an earlier cell on the path (aside from its previous cell). All cells which aren't on the path can then be filled in with walls to yield a valid grid.
In any case, it can then be seen that once becomes large, optimal grids contain repeated instances of the following pattern of walls in the middle:
.#....
...###
###...
....#.
In particular, the answer for is equivalent to the answer for but with this pattern inserted once for a certain interval of its interior columns. In the same way, the answer for every can be determined from the answer for a grid with fewer columns, meaning that only the answers for must be computed from scratch.
Comments