This is an emulator question. Just try the answer so many times and you can solve it. However there are some tips as follow.
(1) If you want to turn off all lights on the first row, you should press the button on the second row to affect it and so on.
(2) the status of lights on the 5th row depend on the initial status of the first row, so just try to change the status of all lights on the first row as much as possible. The maximum try is 2^6 = 64;
#include<stdio.h>
#include<iostream>
#include<string.h>
int press[7][8], puzzle[7][8];
void emulator();
bool guess();
int main(){
int n, sum = 0;
scanf("%d", &n);
while(n--){
memset(puzzle, 0, sizeof(puzzle));
memset(press, 0, sizeof(press));
for(int i = 1; i <= 5; i++){
for(int j = 1; j <= 6; j++){
scanf("%d",&puzzle[i][j]);
}
}
emulator();
printf("PUZZLE #%d\n", ++sum);
for(int i = 1; i <= 5; i++){
for(int j = 1; j <=6; j++){
if(j == 6){
printf("%d", press[i][j]);
}else{
printf("%d ", press[i][j]);
}
}
printf("\n");
}
}
}
void emulator(){ // emulate all the possible status of all lights on the fist row
for(int i = 1; i <= 64; i++){
int pos = 1;
press[1][pos]++;
while(press[1][pos] > 1){
press[1][pos++] = 0;
press[1][pos]++;
}
press[1][7] = 0;
if(guess() == true){
break;
}
}
}
bool guess(){ // judge whether it can make all the lights on the map turn off. Pay much more attention on the lights of last row
for(int i = 2;i <= 5; i++){
for(int j = 1;j <= 6; j++){
press[i][j] = (press[i-1][j] + press[i-2][j] + press[i-1][j-1] + press[i-1][j+1] + puzzle[i-1][j]) % 2;
}
}
int pos = 5;
for(int j = 1; j <= 6; j++){
if(press[pos][j] != (press[pos-1][j] + press[pos][j-1] + press[pos][j+1] + puzzle[pos][j]) % 2){
return false;
}
}
return true;
}