Editorial for WC '17 Finals S5 - Crop Rectangles
Submitting an official solution before solving the problem yourself is a bannable offence.
With each output depending only on a pair of integers , it's safe to guess that some sort of patterns can be found for small values of which can extend to arbitrarily large values. It may be tempting to try solving various small cases by hand to look for such patterns, and it's simple to do so for some (such as when one dimension is equal to or ), but overall it proves far too difficult to do so for others. So, our best bet will be to write some code which can (inefficiently) find answers for small grids, and then look for patterns in its results.
The most standard approach is with breadth-first search, in which the state includes information about which subset of cells have been visited, what cell the lawnmower is currently in, what direction it's facing in, and whether or not it turned just before its last move. This gives us states, with up to transitions from each state.
Many states are unreachable, so we'll want to hash states to track their reachability rather than maintaining a boolean array of that size. This sort of BFS ends up running in a reasonable amount of time (under a minute) for any grid such that is at most or so, which will suffice for our purposes.
Now, let's talk about the patterns which surface. For convenience, let and , and let's talk in terms of the minimum number of "wasted" moves (such that the answer is equal to ). If we run our BFS for all possible grids with , we can come up with the following observations:
- When , .
- When , it's impossible.
- When and , it's impossible.
- When and , there's no clear pattern?
- When , . If we examine the optimal lawnmower path for a grid, it's relatively clear that it can be easily stretched out to arbitrarily large values of , and that larger values of can't help improve it.
- When , . If we examine the optimal lawnmower path for a grid, it's relatively clear that it can be extended to larger grid sizes without wasting any additional moves (by either stretching it out along one dimension or repeatedly wrapping around the outside to increase both dimensions), and that larger grid sizes can't help improve it.
The only problematic case is when and , which requires further investigation. It's once again safe to guess that a pattern will emerge for relatively small values of which can extend to arbitrarily large values. To search for the pattern, we can try running our BFS again for as many grids as possible, which works reasonably up to around .
Unfortunately, this is insufficient for the pattern to become completely obvious, but it is enough to guess it correctly. The values for are unpredictable (), but starting at they finally start to repeat regularly (). To gain full confidence, it's also possible to write a more specialized brute force algorithm for grids (or even use dynamic programming) to compute all values up to much larger values of , though that's less viable in the limited contest time.
With that, all possible cases have been covered! For and , we can either run our BFS or hardcode the values listed above, and for all other cases we have closed-form formulae for the answers.
Comments