There is no need to test all cases. The ideal case is a swiss cheese in which no guest share a wall. Just start filling rooms from there if needed (the only problems is that room filling must follow an order, which makes the code quite large (but repetitive and boring), and there are two base cases for the swiss cheese structure). Maybe it's possible to create a function and decrease code size, but unless you see it at the moment that you are coding it, it would mean loosing too much time during competition when a Ctrl+C, Ctrl+V would be faster (and better than testing all cases).
Currently, my solutions and this one are the only that don't use brute force, but this one is the smallest. Congrats. :) (I was able to create a smaller solution after looking at your code.)
1966 Solvers
Is my wife right? Now with even more wrong husband
1242 Solvers
Back to basics 21 - Matrix replicating
1052 Solvers
98 Solvers
Make a random, non-repeating vector.
2822 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!