Java Algorithm Turn on the Lights Game

Question description

There are 9 lights and 9 switches, all numbered 1 to 9.

Each switch can control several lights. Pressing it once will change the state of the light it controls (from bright to off, from off to bright).

details as follows:

The first switch controls the second and fourth lights;

The second switch controls the first, third, and fifth lights;

The third switch controls the second and sixth lights;

The fourth switch controls the first, fifth, and seventh lights;

The fifth switch controls the second, fourth, sixth, and eighth lights;

The sixth switch controls the third, fifth, and ninth lights;

The seventh switch controls the fourth and eighth lights;

The eighth switch controls the fifth, seventh, and ninth lights;

The ninth switch controls the sixth and eighth lights.

At the beginning, all the lights are off and the switches are turned off. It is required to press several switches so that only 4 lights are on.

Input
Input description:
  Output all possible solutions, one solution per line, each line has 9 characters, from left to right The i character represents the status of the i-th switch ("0" means closed, "1" means open), and is output in dictionary order. The sample output below is only part of the scenario.
Input example:
000001011
000001110
000001111

output

Output description:

Output sample:

HINT: Time limit: 1.0s Memory limit: 256.0MB

Problem-solving ideas

Arrange the states of switches 1 to 9, and then change the lights according to the effect of each switch. Finally, when the number of lights is judged to be 4, output the current state of the switch.

code

public class Main {
    
    
    static final int LEN = 10;
    static int[] arr = new int[LEN];
    static int[] b;
    public static void main(String[] args) {
    
    
        dfs(1);         //启动程序
    }
    private static void dfs(int index) {
    
    
        if (index >= LEN) {
    
    
            display();
            return;
        }
        arr[index] = 0;
        dfs(index + 1);
        arr[index] = 1;
        dfs(index + 1);
    }

    private static void display() {
    
    
        b = new int[LEN];
        for (int i = 1; i < LEN; i++) {
    
    
            if (arr[i] == 1)        //若开关为开的状态 判断灯的属性
                change(i);
        }
        int sum = 0;                //定义灯亮的数量
        for (int i = 1; i < LEN; i++) {
    
    
            if (b[i] == 1)
                sum++;
            if (sum > 4)
                return;
        }
        if (sum == 4) {
    
    
            for (int i = 1; i < LEN; i++) {
    
    
                System.out.print(arr[i]);       //输出为4时的组合
            }
            System.out.println();
        }
    }

    private static void change(int i) {
    
             //依次判断当打开开关时灯的状态
        switch (i) {
    
    
            case 1:
                b[2] = b[2] == 0 ? 1 : 0;
                b[4] = b[4] == 0 ? 1 : 0;
                break;
            case 2:
                b[1] = b[1] == 0 ? 1 : 0;
                b[3] = b[3] == 0 ? 1 : 0;
                b[5] = b[5] == 0 ? 1 : 0;
                break;
            case 3:
                b[2] = b[2] == 0 ? 1 : 0;
                b[6] = b[6] == 0 ? 1 : 0;
                break;
            case 4:
                b[1] = b[1] == 0 ? 1 : 0;
                b[5] = b[5] == 0 ? 1 : 0;
                b[7] = b[7] == 0 ? 1 : 0;
                break;
            case 5:
                b[2] = b[2] == 0 ? 1 : 0;
                b[4] = b[4] == 0 ? 1 : 0;
                b[6] = b[6] == 0 ? 1 : 0;
                b[8] = b[8] == 0 ? 1 : 0;
                break;
            case 6:
                b[3] = b[3] == 0 ? 1 : 0;
                b[5] = b[5] == 0 ? 1 : 0;
                b[9] = b[9] == 0 ? 1 : 0;
                break;
            case 7:
                b[4] = b[4] == 0 ? 1 : 0;
                b[8] = b[8] == 0 ? 1 : 0;
                break;
            case 8:
                b[5] = b[5] == 0 ? 1 : 0;
                b[7] = b[7] == 0 ? 1 : 0;
                b[9] = b[9] == 0 ? 1 : 0;
                break;
            case 9:
                b[6] = b[6] == 0 ? 1 : 0;
                b[8] = b[8] == 0 ? 1 : 0;
                break;
        }
    }
}

Guess you like

Origin blog.csdn.net/joreng/article/details/123874522