I've come across the following task from z-trening (http://www.z-trening.com/tasks.php?show_task=5000000148). First I thought it would be easy with backtracking since sides where small. But I'm getting TLE in two cases, and when trying to solve a 5x5 square with all its squares empty it takes forever to solve it.
Is there a more efficient way to solve it or any useful heuristic?
This code passed 17/20 cases: http://pastie.org/2073017
This code passed 16/20 cases: http://pastie.org/2073028
AC solution: http://pastie.org/2073682
If N is odd: just look at the pictures: link
ignore