|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.Set; |
| 5 | +import java.util.StringTokenizer; |
| 6 | + |
| 7 | +public class BJ_17135_캐슬_디펜스 { |
| 8 | + |
| 9 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + private static StringTokenizer st; |
| 12 | + |
| 13 | + private static int N, M, D, ans; |
| 14 | + private static int[] archers; |
| 15 | + private static int[][] map, grid; |
| 16 | + private static Set<Integer> targets; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + init(); |
| 20 | + sol(); |
| 21 | + |
| 22 | + bw.write(ans + ""); |
| 23 | + bw.flush(); |
| 24 | + bw.close(); |
| 25 | + br.close(); |
| 26 | + } |
| 27 | + |
| 28 | + private static void init() throws IOException { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + N = Integer.parseInt(st.nextToken()); |
| 31 | + M = Integer.parseInt(st.nextToken()); |
| 32 | + D = Integer.parseInt(st.nextToken()); |
| 33 | + |
| 34 | + map = new int[N][M]; |
| 35 | + grid = new int[N][M]; |
| 36 | + |
| 37 | + for (int i = 0; i < N; i++) { |
| 38 | + st = new StringTokenizer(br.readLine()); |
| 39 | + for (int j = 0; j < M; j++) { |
| 40 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + archers = new int[3]; |
| 45 | + targets = new HashSet<>(); |
| 46 | + } |
| 47 | + |
| 48 | + private static void sol() { |
| 49 | + for (int i = 0; i < M; i++) { |
| 50 | + for (int j = i + 1; j < M; j++) { |
| 51 | + for (int k = j + 1; k < M; k++) { |
| 52 | + archers[0] = i; |
| 53 | + archers[1] = j; |
| 54 | + archers[2] = k; |
| 55 | + |
| 56 | + ans = Math.max(ans, simulate()); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private static int simulate() { |
| 63 | + for (int i = 0; i < N; i++) { |
| 64 | + grid[i] = map[i].clone(); |
| 65 | + } |
| 66 | + |
| 67 | + int killed = 0; |
| 68 | + for (int archerRow = N; archerRow >= 1; archerRow--) { |
| 69 | + targets.clear(); |
| 70 | + |
| 71 | + for (int i = 0; i < 3; i++) { |
| 72 | + findTarget(archerRow, archers[i]); |
| 73 | + } |
| 74 | + |
| 75 | + for (int target : targets) { |
| 76 | + int row = target / M; |
| 77 | + int col = target % M; |
| 78 | + if (grid[row][col] == 1) { |
| 79 | + grid[row][col] = 0; |
| 80 | + killed++; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return killed; |
| 86 | + } |
| 87 | + |
| 88 | + private static void findTarget(int archerRow, int archerCol) { |
| 89 | + int targetRow = -1; |
| 90 | + int targetCol = -1; |
| 91 | + int minDist = Integer.MAX_VALUE; |
| 92 | + |
| 93 | + for (int row = archerRow - 1; row >= 0; row--) { |
| 94 | + for (int col = 0; col < M; col++) { |
| 95 | + if (grid[row][col] == 0) continue; |
| 96 | + |
| 97 | + int dist = Math.abs(archerRow - row) + Math.abs(archerCol - col); |
| 98 | + if (dist > D) continue; |
| 99 | + |
| 100 | + if (dist < minDist || (dist == minDist && col < targetCol)) { |
| 101 | + minDist = dist; |
| 102 | + targetRow = row; |
| 103 | + targetCol = col; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + if (targetRow != -1) { |
| 109 | + targets.add(targetRow * M + targetCol); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | +} |
| 114 | +``` |
0 commit comments