|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.IOException; |
| 3 | +import java.io.InputStreamReader; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class JM_나무박멸 { |
| 7 | + static int N; |
| 8 | + static int M; // 박멸이 진행되는 년 수 |
| 9 | + static int K; // 제초제의 확산 범위 K |
| 10 | + static int C; // 제초제가 남아있는 년 수 C |
| 11 | + static int[][] map; // 0(빈칸), -1(벽) |
| 12 | + static final int[][] DIR = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; |
| 13 | + static int[][] herbicide; |
| 14 | + |
| 15 | + private static boolean inRange(int y, int x) { |
| 16 | + return 0 <= y && y < N && 0 <= x && x < N; |
| 17 | + } |
| 18 | + |
| 19 | + private static void reduceHerbicide() { |
| 20 | + for (int i = 0; i < N; i++) { |
| 21 | + for (int j = 0; j < N; j++) { |
| 22 | + if(herbicide[i][j] <= 0) continue; |
| 23 | + herbicide[i][j] -= 1; |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + private static int[] findMaxRemovedTreePos() { |
| 29 | + int[] pos = new int[3]; |
| 30 | + for (int i = 0; i < N; i++) { |
| 31 | + for (int j = 0; j < N; j++) { |
| 32 | + if(map[i][j] <= 0) continue; |
| 33 | + |
| 34 | + int removedTree = map[i][j]; |
| 35 | + for (int d = 4; d < 8; d++) { |
| 36 | + int ny = i; |
| 37 | + int nx = j; |
| 38 | + int k = 0; |
| 39 | + while (k++ < K) { |
| 40 | + ny += DIR[d][0]; |
| 41 | + nx += DIR[d][1]; |
| 42 | + if(!inRange(ny, nx) || map[ny][nx] == 0 || map[ny][nx] == -1) break; |
| 43 | + removedTree += map[ny][nx]; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if(removedTree > pos[2]) { |
| 48 | + pos[0] = i; |
| 49 | + pos[1] = j; |
| 50 | + pos[2] = removedTree; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + return pos; |
| 55 | + } |
| 56 | + |
| 57 | + private static int sprayHerbicide() { |
| 58 | + int[] pos = findMaxRemovedTreePos(); |
| 59 | + int y = pos[0]; |
| 60 | + int x = pos[1]; |
| 61 | + int removedTree = pos[2]; |
| 62 | + |
| 63 | + map[y][x] = 0; |
| 64 | + herbicide[y][x] = C + 1; |
| 65 | + for (int d = 4; d < 8; d++) { |
| 66 | + int ny = y; |
| 67 | + int nx = x; |
| 68 | + int k = 0; |
| 69 | + while (k++ < K) { |
| 70 | + ny += DIR[d][0]; |
| 71 | + nx += DIR[d][1]; |
| 72 | + if(!inRange(ny, nx)) break; |
| 73 | + herbicide[ny][nx] = C + 1; |
| 74 | + if(map[ny][nx] <= 0) break; |
| 75 | + map[ny][nx] = 0; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return removedTree; |
| 80 | + } |
| 81 | + |
| 82 | + private static int countReproduction(int y, int x) { |
| 83 | + int cnt = 0; |
| 84 | + for (int k = 0; k < 4; k++) { |
| 85 | + int ny = y + DIR[k][0]; |
| 86 | + int nx = x + DIR[k][1]; |
| 87 | + if(!inRange(ny, nx) || map[ny][nx] != 0 || herbicide[ny][nx] > 0) continue; |
| 88 | + cnt++; |
| 89 | + } |
| 90 | + return cnt == 0 ? 0 : map[y][x] / cnt; |
| 91 | + } |
| 92 | + |
| 93 | + private static void reproduce() { |
| 94 | + int[][] tmp = new int[N][N]; |
| 95 | + for (int i = 0; i < N; i++) { |
| 96 | + for (int j = 0; j < N; j++) { |
| 97 | + tmp[i][j] = map[i][j]; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + for (int i = 0; i < N; i++) { |
| 102 | + for (int j = 0; j < N; j++) { |
| 103 | + if(map[i][j] <= 0) continue; |
| 104 | + |
| 105 | + int reproduction = countReproduction(i, j); |
| 106 | + if(reproduction == 0) continue; |
| 107 | + for (int d = 0; d < 4; d++) { |
| 108 | + int ny = i + DIR[d][0]; |
| 109 | + int nx = j + DIR[d][1]; |
| 110 | + if(!inRange(ny, nx) || map[ny][nx] != 0 || herbicide[ny][nx] > 0) continue; |
| 111 | + tmp[ny][nx] += reproduction; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + map = tmp; |
| 116 | + } |
| 117 | + |
| 118 | + private static void growUp() { |
| 119 | + for (int i = 0; i < N; i++) { |
| 120 | + for (int j = 0; j < N; j++) { |
| 121 | + if(map[i][j] <= 0) continue; |
| 122 | + for (int d = 0; d < 4; d++) { |
| 123 | + int ny = i + DIR[d][0]; |
| 124 | + int nx = j + DIR[d][1]; |
| 125 | + if(!inRange(ny, nx) || map[ny][nx] <= 0) continue; |
| 126 | + map[i][j] += 1; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private static int solve() { |
| 133 | + int removedTree = 0; |
| 134 | + |
| 135 | + while (M-- > 0) { |
| 136 | + reduceHerbicide(); |
| 137 | + growUp(); |
| 138 | + reproduce(); |
| 139 | + removedTree += sprayHerbicide(); |
| 140 | + } |
| 141 | + |
| 142 | + return removedTree; |
| 143 | + } |
| 144 | + |
| 145 | + public static void main(String[] args) throws IOException { |
| 146 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 147 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 148 | + N = Integer.parseInt(st.nextToken()); |
| 149 | + M = Integer.parseInt(st.nextToken()); |
| 150 | + K = Integer.parseInt(st.nextToken()); |
| 151 | + C = Integer.parseInt(st.nextToken()); |
| 152 | + |
| 153 | + herbicide = new int[N][N]; |
| 154 | + map = new int[N][N]; |
| 155 | + for (int i = 0; i < N; i++) { |
| 156 | + st = new StringTokenizer(br.readLine()); |
| 157 | + for (int j = 0; j < N; j++) { |
| 158 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 159 | + } |
| 160 | + } |
| 161 | + System.out.println(solve()); |
| 162 | + } |
| 163 | +} |
0 commit comments