|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +public class Main { |
| 5 | + |
| 6 | + static int N, M, D, maxEnemy; |
| 7 | + static int[][] original; |
| 8 | + static int[] archerCols; |
| 9 | + |
| 10 | + public static void main(String[] args) throws IOException { |
| 11 | + input(); |
| 12 | + archerCols = new int[3]; |
| 13 | + maxEnemy = 0; |
| 14 | + combination(0, 0); |
| 15 | + System.out.println(maxEnemy); |
| 16 | + } |
| 17 | + static void combination(int start, int idx) { |
| 18 | + if (idx == 3) { |
| 19 | + maxEnemy = Integer.max(maxEnemy, play()); |
| 20 | + return; |
| 21 | + } |
| 22 | + for (int i = start; i < M; i++) { |
| 23 | + archerCols[idx] = i; |
| 24 | + combination(i + 1, idx + 1); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static int play() { |
| 29 | + // 게임을 플레이 할 임시 격자판 생성 |
| 30 | + int[][] board = new int[N][M]; |
| 31 | + for (int i = 0; i < N; i++) { |
| 32 | + for (int j = 0 ; j < M; j++) { |
| 33 | + board[i][j] = original[i][j]; |
| 34 | + } |
| 35 | + } |
| 36 | + int total = 0; |
| 37 | + do { |
| 38 | + total += shoot(board); |
| 39 | + moveEnemy(board); |
| 40 | + } while(!isEmpty(board)); |
| 41 | + return total; |
| 42 | + } |
| 43 | + |
| 44 | + static void moveEnemy(int[][] board) { |
| 45 | + for (int r = N-2; r > -1; r--) { |
| 46 | + for (int c = 0; c < M; c++) { |
| 47 | + board[r+1][c] = board[r][c]; |
| 48 | + } |
| 49 | + } |
| 50 | + // 맨 위 행은 0으로 채운다. |
| 51 | + Arrays.fill(board[0], 0); |
| 52 | + } |
| 53 | + static int shoot(int[][] board) { |
| 54 | + Set<Integer> killed = new HashSet<>(); |
| 55 | + for (int archerCol : archerCols) { |
| 56 | + int minDist = D+1; |
| 57 | + int minLoc = -1; |
| 58 | + for (int r = 0; r < N; r++) { |
| 59 | + for (int c = 0; c < M; c++) { |
| 60 | + if (board[r][c] == 0) continue; |
| 61 | + int dist = getDist(N, archerCol, r, c); |
| 62 | + if (dist <= D) { |
| 63 | + if (dist < minDist || (dist == minDist && c < minLoc % M)) { |
| 64 | + minDist = dist; |
| 65 | + minLoc = r * M + c; |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + if (minLoc == -1) { continue; } |
| 71 | + killed.add(minLoc); |
| 72 | + } |
| 73 | + |
| 74 | + // 제거한 적 위치 0으로 전환 |
| 75 | + for (int k : killed) { |
| 76 | + int kr = k / M; |
| 77 | + int kc = k % M; |
| 78 | + board[kr][kc] = 0; |
| 79 | + } |
| 80 | + return killed.size(); |
| 81 | + } |
| 82 | + |
| 83 | + static boolean isEmpty(int[][] board) { |
| 84 | + for (int i = 0; i < N; i++) { |
| 85 | + for (int j = 0; j < M; j++) { |
| 86 | + if (board[i][j] == 1) { return false; } |
| 87 | + } |
| 88 | + } |
| 89 | + return true; |
| 90 | + } |
| 91 | + static int getDist(int r1, int c1, int r2, int c2) { |
| 92 | + return Math.abs(r1-r2) + Math.abs(c1-c2); |
| 93 | + } |
| 94 | + static void input() throws IOException { |
| 95 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 96 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 97 | + N = Integer.parseInt(st.nextToken()); |
| 98 | + M = Integer.parseInt(st.nextToken()); |
| 99 | + D = Integer.parseInt(st.nextToken()); |
| 100 | + original = new int[N][M]; |
| 101 | + for (int i = 0; i < N; i++) { |
| 102 | + st = new StringTokenizer(br.readLine()); |
| 103 | + for (int j = 0; j < M; j++) { |
| 104 | + original[i][j] = Integer.parseInt(st.nextToken()); |
| 105 | + } |
| 106 | + } |
| 107 | + br.close(); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +``` |
0 commit comments