I am trying to find the solution of lights out game using backtracking method. I am not able to understand the algorithm for this process. My approach is to enumerate all integers from 0 to 2n2 - 1 and For each integer convert it into a binary number which has n*n bits. Then, separate it into n2 binary digits (0 for light off, 1 for light on) and assign them into a n × n grid, for example: I have written the following code:-
void find_solution(int dest[][MAX_SIZE], int size) {
int y = pow(size,size);
int remainder;
for (int x = 0; x<pow(2,y); x++){
int i = 1;
int binary_number = 0;
int n = x;
while (n!=0) {
remainder = n%2;
n/=2;
binary_number += remainder*i;
i *= 10;
}
int binary_number_digits[size][size];
for (int k = 0; k<size; k++) {
for (int l = 0; l<size; l++) {
binary_number_digits[k][l] = binary_number%10;
binary_number/=10;
}
}
int count = 0;
for (int i = 0; i<size; i++) {
for (int j = 0; j<size; j++) {
if (binary_number_digits[i][j] == dest[i][j]) {
count++;
}
if (count <= 4 && count > 0) {
if (binary_number_digits[i][j] == 1) {
cout << i << j;
}
}
}
}
}
}
I have converted the decimal digits to binary numbers and stored it in an array and checking if they match the randomly generated n*n grid. If it is a 1, it prints that coordinate(x,y). Anyone could please help me solve the problem with this algorithm. Thanks!