Editorial for COCI '06 Contest 1 #6 Debug


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

To solve the problem efficiently, observe that, if square A is a killer, then every square inside it (sharing the same center point) is also a killer. Conversely, if a square is not a killer then no square surrounding it can be a killer.

This forms the basis of an \mathcal O(N^4) solution: try and extend a square from each memory cell. The worst-case complexity is \mathcal O(N^4) because there are N^2 cells, \mathcal O(N) dimensions to be checked and checking the frame (when trying to extend a square killer) takes \mathcal O(N).

Naively implementing the above approach will not be efficient enough. One way of speeding it up is to, for each cell and each of the 4 directions, precalculate 32 or 64 binary digits starting at the cell (y,x) in the chosen direction. This allows us to check frames in blocks of 32 or 64 bits (instead of one by one) and speed up the solution by a factor.

Other heuristics that reduce the number of checks (eliminating those that surely can't contribute to the solution) could be used to speed up the solution further.


Comments

There are no comments at the moment.