|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +public class BJ_1799_비숍 { |
| 7 | + |
| 8 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 10 | + |
| 11 | + private static int N, blackAns, whiteAns; |
| 12 | + private static boolean[] diag1, diag2; // 각각 \, / 대각선 비교 |
| 13 | + private static List<Point> black, white; |
| 14 | + |
| 15 | + private static class Point { |
| 16 | + int x; |
| 17 | + int y; |
| 18 | + |
| 19 | + Point(int x, int y) { |
| 20 | + this.x = x; |
| 21 | + this.y = y; |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + public static void main(String[] args) throws IOException { |
| 26 | + init(); |
| 27 | + sol(); |
| 28 | + } |
| 29 | + |
| 30 | + private static void init() throws IOException { |
| 31 | + N = Integer.parseInt(br.readLine()); |
| 32 | + |
| 33 | + diag1 = new boolean[2 * N]; |
| 34 | + diag2 = new boolean[2 * N]; |
| 35 | + black = new ArrayList<>(); |
| 36 | + white = new ArrayList<>(); |
| 37 | + |
| 38 | + for (int i = 0; i < N; i++) { |
| 39 | + String s = br.readLine(); |
| 40 | + for (int j = 0; j < N; j++) { |
| 41 | + char c = s.charAt(j * 2); |
| 42 | + |
| 43 | + if (c == '1') { |
| 44 | + if ((i + j) % 2 == 0) { |
| 45 | + black.add(new Point(i, j)); |
| 46 | + } else { |
| 47 | + white.add(new Point(i, j)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static void sol() throws IOException { |
| 55 | + // 검은색 칸만 탐색 |
| 56 | + blackAns = 0; |
| 57 | + dfs(black, 0, 0); |
| 58 | + |
| 59 | + // 흰색 칸만 탐색 |
| 60 | + whiteAns = 0; |
| 61 | + dfs(white, 0, 0); |
| 62 | + |
| 63 | + bw.write((blackAns + whiteAns) + ""); |
| 64 | + bw.flush(); |
| 65 | + bw.close(); |
| 66 | + br.close(); |
| 67 | + } |
| 68 | + |
| 69 | + private static void dfs(List<Point> list, int depth, int cnt) { |
| 70 | + if (list == black) { |
| 71 | + blackAns = Math.max(blackAns, cnt); |
| 72 | + } else { |
| 73 | + whiteAns = Math.max(whiteAns, cnt); |
| 74 | + } |
| 75 | + |
| 76 | + for (int i = depth; i < list.size(); i++) { |
| 77 | + Point p = list.get(i); |
| 78 | + // \은 x - y가 동일함 - 음수가 될 수 있으므로 N을 더해줌 |
| 79 | + // /은 x + y가 동일함 |
| 80 | + int d1 = p.x - p.y + N; |
| 81 | + int d2 = p.x + p.y; |
| 82 | + |
| 83 | + // 해당 대각선이 이미 탐색되었으면 pass |
| 84 | + if (diag1[d1] || diag2[d2]) continue; |
| 85 | + |
| 86 | + diag1[d1] = true; |
| 87 | + diag2[d2] = true; |
| 88 | + dfs(list, i + 1, cnt + 1); |
| 89 | + diag1[d1] = false; |
| 90 | + diag2[d2] = false; |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
0 commit comments