Editorial for ECOO '12 R2 P4 - Lo Shudoku
Submitting an official solution before solving the problem yourself is a bannable offence.
Exhaustive Search
An exhaustive search of all possible moves would be difficult to write and will not finish in time, so another strategy is needed. Perhaps a breadth first search would work in some cases, but still in the worst case would still have a bad running time and memory requirements. As a ballpark estimate, in the worst case you have to consider swaps for each square, giving you combinations (more than million) to try.
Observations
If you can find a fast enough way to transform a square into one of the magic squares, you can just run it times – once for each of the eight possible magic squares – and look for the smallest required number of moves.
There is an upper bound of moves for transforming any square that has no blanks into any given magic square. This is because every swap can be used to put at least one number in its correct spot. In fact, there is no faster approach when there are no blanks than to simply cycle through all nine spots and swap the correct piece into place if necessary.
It actually does not matter to the number of moves required whether you fill in the blanks at the beginning or at the end.
Final Strategy
Try each of the magic squares. For each one, you transform the given square by first swapping into place as many pieces as you can, and then by filling in the blanks at the end with their correct numbers. Whichever of the squares requires the least moves gives you the answer.
Comments