1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import java.util.*; public class Main{ static int[][] arr = new int[5][5]; static int[][] backup = new int[5][5]; public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for(int i = 0; i < n; i++){ for(int k = 0; k < 5; k++){ String str = sc.next(); for(int j = 0; j < str.length(); j++){ arr[k][j] = Integer.valueOf(str.charAt(j)+""); } }
System.out.println(work(arr)); } } public static int work(int[][] arr){ int ans = Integer.MAX_VALUE >> 1; for(int i = 0; i < 1 << 5; i ++){ for(int k = 0; k < 5; k ++){ for(int q =0; q < 5; q++){ backup[k][q] = arr[k][q]; } }
int res = 0; for(int j = 0; j < 5; j++){ if((i >> j & 1) == 1){ res++; turn(backup, 0, j); } } for(int k = 0; k < 4; k++){ for(int j = 0; j < 5; j++){ if(backup[k][j] == 0){ res++; turn(backup, k+1, j); } } } boolean bool = true; for(int j = 0; j < 5; j++){ if(backup[4][j] == 0){ bool = false; } } if(bool){ ans = Math.min(ans, res); } } return ans > 6 ? -1 : ans; } static int[] dx = new int[]{0, -1, 0, 1, 0}; static int[] dy = new int[]{0, 0, 1, 0, -1}; public static void turn(int[][] arr, int x, int y){ for(int k = 0; k < 5; k++){ int a = x + dx[k]; int b = y + dy[k]; if(a >= 0 && a < 5 && b >= 0 && b < 5){ arr[a][b] = arr[a][b] == 0 ? 1 : 0; } } } }
|