AcWing 116. Pilot brother (enumeration)

Keywords: Java

This question seems to have nothing to do with the pilot and his brother

Topic
Problem

The problem of pressing the button is similar, but it is more troublesome. Pressing the button once will link the whole row and column. There is no particularly good way to sort out a formula or anything to solve it. The problem surface is a 4 * 4 square. You may consider the violent enumeration method, because it is impossible to press a button twice, otherwise it is meaningless, so there are only 216 choices, plus judgment, reverse one Some operations, the complexity is acceptable.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Main {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter pw = new PrintWriter(System.out);
    static char g[][] = new char[4][4], backup[][] = new char[4][4];
    static ArrayList<int[]> al = new ArrayList<>();

    public static void main(String[] args) throws IOException {

        for (int i = 0; i < 4; i++) {
            String s = br.readLine();
            for (int j = 0; j < 4; j++)
                g[i][j] = s.charAt(j);
        }

        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 1 << 16; i++) {

            for (int j = 0; j < 4; j++)
                for (int k = 0; k < 4; k++)
                    backup[j][k] = g[j][k];

            for (int j = 0; j < 16; j++) {
                if (((i >> j) & 1) == 1) {
                    int col = j % 4; //1
                    int row = j / 4; //0
                    for (int cols = 0; cols < 4; cols++)
                        if (backup[cols][col] == '+')
                            backup[cols][col] = '-';
                        else
                            backup[cols][col] = '+';

                    for (int rows = 0; rows < 4; rows++)
                        if (backup[row][rows] == '+')
                            backup[row][rows] = '-';
                        else
                            backup[row][rows] = '+';
                    if (backup[row][col] == '+') backup[row][col] = '-';
                    else backup[row][col] = '+';
                }
            }
            //check
            if (check()) {
                int num = numOf1(i);
                if (num < min) {
                    min = num;
                    al.clear();
                    for (int j = 0; j < 16; j++)
                        if (((i >> j) & 1) == 1)
                            al.add(new int[]{j / 4 + 1, j % 4 + 1});
                }
            }
        }

        pw.println(min);
        for (int i = 0; i < al.size(); i++) {
            int temp[] = al.get(i);
            pw.println(temp[0] + " " + temp[1]);
        }

        pw.flush();
        pw.close();
        br.close();
    }

    public static boolean check() {
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                if (backup[i][j] == '+')
                    return false;
        return true;
    }

    public static int numOf1(int n) {
        int ans = 0;
        while (n != 0) {
            n -= lowbit(n);
            ans++;
        }
        return ans;
    }

    private static int lowbit(int n) {
        return n & -n;
    }

}

Published 125 original articles, won praise 3, visited 1582
Private letter follow

Posted by bostonmacosx on Thu, 30 Jan 2020 09:01:26 -0800