The problem statement
Since we all rely on mother earth, it’s our duty to save her. Therefore, you’re now asked to save energy by switching lights off.
A friend of yours has the following problem in his job. There’s a grid of size 10x10, where each square has a light bulb and a light switch attached to it. Unfortunately, these lights don’t work as they are supposed to. Whenever a switch is pressed, not only its own bulb is switched, but also the ones left, right, above and under it. Of course if a bulb is on the edge of the grid, there are fewer bulbs switched.
When a light switches it means it’s now on if it was off before and it’s now off if it was on before. Look at the following examples, which show only a small part of the whole grid. They show what happens if the middle switch is pressed. ‘ O ’ stands for a light that’s on, ‘ # ’ stands for a light that’s off.
### #O# ### -> OOO ### #O# ### #O# OOO -> ### ### #O#your friend loves to save energy and asks you to write a program that finds out if it is possible to turn all the lights off and if possible then how many times he has to press switches in order to turn all the lights off.
The Idea
The idea to solve this problem is to try all possible combination for the first row and then chase down to turn all off. If you find a better solution in the process then store that.
Here chase means, if a light is on at (i - 1, j) switch at (i, j) so as to make sure all the lights on (i - 1)th row will be turned off except for the last row.
By try all possible combination we will able to find the answer. But one thing I don't understand is, what is the correctness of this problem ? How can we guaranteed that this indeed touches the optimal solution. Is there any prove for this ?
For better understanding of the problem try this game.