UVa 10309 - Turn the Lights Off


 * 10309 - Turn the Lights Off

Summary
You are given a 10x10 grid of light bulbs, each one of them is either switched of or on. Each light bulb has a switch on the same cell in the grid cell. Unfortunately, these switches don't work as expected. Whenever a switch is pressed the bulb on the same cell along with the 4 neighboring cells are toggled. We need to find the minimum number of switches pressed in order to turn off the whole grid. ###     #O# ### ->  OOO ###     #O# ###     #O# OOO ->  ### ###     #O#
 * Flash game to illustrate the problem

Explanation
This problem is solved using complete search technique. Notice that each bulb can be toggled by 3 switches on the same row and 1 switch on the upper row and another one on the lower row, keep this in mind as we will use it later. Our solution has three steps: 1. For the first row try all 2^10 possible combinations of switches. We expect to have some switches turned on after this step. 2. For the rest of the rows, toggle switch (i, j) if (i-1, j) is switched on, this means toggle any switch if the bulb above it (in the previous row is turned on). This step insures that all bulbs will be turned off using the switch in the next row, except the last row, as there is no next row. 3. Check the bulbs in the last row, if all bulbs are turned off, then the current solution if valid, if any of the bulbs is on, this solution is not valid. Keep track of the number of switches toggled in each solution, and the final result is their minimum value;

Gotchas

 * Switches on the edges don't toggle 5 bulbs.
 * Don;t forget to print the name of the case in the output.

Implementations

 * Using bits to represent bulbs is better since it has 2 states and is much faster than using booleans or characters.

Input
CASE_1 OO##OOOOO# OO###OO##O OO#O#####O O###OO#O#O OO#OO#O##O CASE_2 O##OOO##OO O##OO###OO O###OO##OO O#O####OOO OOO##OOOOO CASE_3 OO######## CASE_4 OOOO####O# OOOO##O#OO O#O###O#O# OOO#OO#OO# O###OO#### OOO#O#OO## OO###O#### O#OOOO###O CASE_5 OO##O###OO OOOOO#####
 * 1) OO###OO
 * 2) OO#####O
 * 3) OO#OO#OOO
 * 4) O#OO####O
 * 1) O##O##OO
 * 1) OO####O##
 * 1) OOOOO#O#
 * 1) O##OO#O##
 * 1) OOOO###OO
 * 1) OOO##O##
 * 1) OOO#OOOO#
 * 1) OO#OO##OO
 * 1) O#OOO##O#
 * 2) O###O#
 * 1) O####OO##
 * 2) OO##O##O
 * 3) O####O#O#
 * 4) OOO#OO##O
 * 5) O##O#OO
 * 6) OO#O#O

Output
CASE_1 50 CASE_2 45 CASE_3 39 CASE_4 49 CASE_5 48