技巧:
二进制串模型枚举尝试
思路:
二进制串模型枚举尝试
实现:
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); int[][] arr = new int[4][4]; for (int i = 0; i < 4; i++) { char[] chars = br.readLine().toCharArray(); for (int j = 0; j < chars.length; j++) { if ('b' == chars[j]) { arr[i][j] = 1; } } } // ==================================================== int ans = 0x7fffffff; int tryModel = 1 << 16; for (int i = 0; i < tryModel; i++) { if (allSame(flip(arr, i))) { int count = 0; int x = i; while (x != 0) { if ((x & 1) == 1) { count++; } x >>= 1; } ans = count < ans ? count : ans; } } if (ans == 0x7fffffff) { System.out.println("Impossible"); } else { System.out.println(ans); } } static boolean allSame(int[][] tmp) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (tmp[i][j] != tmp[0][0]) { return false; } } } return true; } static int[][] flip(int[][] arr, int k) { int[][] tmp = new int[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { tmp[i][j] = arr[i][j]; } } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if ((k & 1) == 1) { tmp[i][j] ^= 1; if (i > 0) { tmp[i - 1][j] ^= 1; } if (i < 3) { tmp[i + 1][j] ^= 1; } if (j > 0) { tmp[i][j - 1] ^= 1; } if (j < 3) { tmp[i][j + 1] ^= 1; } } k >>= 1; } } return tmp; } }