lights out problem

Topic description:

  There is a matrix of buttons with 6 buttons in each row for 5 rows. There is a light at the location of each button. When a button is pressed, the button and the surrounding position (upper, lower, left, right) lights will change once.

  Question: Given the initial state of a set of lights, find a solution for all lights to go out.

 

Topic Analysis:

- If the light was originally on, it will be extinguished;

--If the light was originally off, it will be lit;

--A light is pressed even number of times, it means that the state has not changed;

--The button on the corner of the matrix changes the state of the 3 lights;

--The button on the side of the matrix changes the state of the 4 lights;

--Other buttons change the state of 5 lights.

See below:

  

The essence of the problem: enumeration

We can solve this problem in the most clumsy way, that is, for the matrix of lights with 5 rows and 6 columns, we can set up 30 loops to enumerate the states of the lights in turn, and finally we can find the solution to the problem,

It's just that, although feasible, it is only suitable for small-scale lamp matrices (and it is also very computationally expensive). Of course we have a better solution, that is, for an enumeration problem, we

The first thing to think about is whether it can reduce the number of enumerations. In the problem of lights out, we will find that as long as we determine the status of the lights in the first row, the status of the lights in the second row will also be determined.

The state of the lights in the third row is also determined due to the determination of the state of the lights in the second row... In other words, when we determine the state of the lights in the first row (at this time, not necessarily all the lights in the first row are off, but also there are some unextinguished lights),

We hand over the lights in the first row to the button of the second row of lights to control them to turn them off, so that the button status of the second row of lights is determined by the status of the lights in the first row. Similarly, the third and fourth rows It is also determined. when

We need to change the state of the lights in the 5th row to turn off the entire matrix of lights, the lights in the first three rows have been turned off, and if the button state of the lights in the fifth row turns off the lights in the fourth and fifth rows, then this group ( from the first line

To the fifth row) button press method is the solution.

 

Question Skills:

Many people first apply for three two-dimensional arrays (an initial light state, a current test switch light state, and a result matrix) after thinking about it. However, after careful consideration, we will find that we use

The numbers that arrive are only "0" and "1", and computers are particularly good at processing 01 strings, and our various basic types are represented by binary. So, suppose we declare a variable of type char:

char ch; then we have an eight-byte array (loaded with 8-bit binary numbers). Therefore, we can apply for three one-dimensional arrays of char type to store the lamp matrix and the result matrix.

We can use bit operations to change the state of the light button.

This is the problem analysis. For the complete code, please see my column example code--lights out problem.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324883432&siteId=291194637