|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | + |
| 6 | +public class Main { |
| 7 | + static int[] map = new int[10]; |
| 8 | + static int sum; |
| 9 | + static int minToggle = Integer.MAX_VALUE; |
| 10 | + |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 13 | + |
| 14 | + for (int i = 0; i < 10; i++) { |
| 15 | + String input = br.readLine(); |
| 16 | + for (int j = 0; j < 10; j++) { |
| 17 | + if (input.charAt(j) == 'O') { |
| 18 | + map[i] |= (1 << j); |
| 19 | + } |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + for (int firstRow = 0; firstRow < (1 << 10); firstRow++) { |
| 24 | + int[] tempMap = map.clone(); |
| 25 | + int toggleCount = 0; |
| 26 | + |
| 27 | + for (int j = 0; j < 10; j++) { |
| 28 | + if (isLightOn(firstRow, j)) { |
| 29 | + toggle(tempMap, 0, j); |
| 30 | + toggleCount++; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + for (int i = 1; i < 10; i++) { |
| 35 | + for (int j = 0; j < 10; j++) { |
| 36 | + if (isLightOn(tempMap[i - 1], j)) { |
| 37 | + toggle(tempMap, i, j); |
| 38 | + toggleCount++; |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + if (tempMap[9] == 0) { |
| 44 | + minToggle = Math.min(minToggle, toggleCount); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + System.out.println(minToggle == Integer.MAX_VALUE ? -1 : minToggle); |
| 49 | + } |
| 50 | + |
| 51 | + static boolean isLightOn(int row, int col) { |
| 52 | + return (row & (1 << col)) != 0; |
| 53 | + } |
| 54 | + |
| 55 | + static void toggle(int[] map, int x, int y) { |
| 56 | + map[x] ^= (1 << y); |
| 57 | + |
| 58 | + if (x > 0) map[x - 1] ^= (1 << y); |
| 59 | + if (x < 9) map[x + 1] ^= (1 << y); |
| 60 | + if (y > 0) map[x] ^= (1 << (y - 1)); |
| 61 | + if (y < 9) map[x] ^= (1 << (y + 1)); |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
0 commit comments