|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +/* |
| 5 | + * ์กฐ์ฌํด์ผ ๋๋ ๋ถ๋ถ |
| 6 | + * - ๋๋ฌด๊ฐ ์ฑ์ฅํ๊ณ ๋ฒ์ํ ๋, ๋ฒฝ์ ์ ๋ณด๊ฐ ์์ด์ง๋ฉด ์๋์! |
| 7 | + */ |
| 8 | + |
| 9 | +public class DH_๋๋ฌด๋ฐ๋ฉธ { |
| 10 | + |
| 11 | + static int N, M, K, C; |
| 12 | + static int totalRemoveTreeCnt, tmpRemoveTreeCnt; |
| 13 | + static int[][] map, remove; // map: ๋๋ฌด, remove: ์ ์ด์ ์ง์ ์๊ฐ |
| 14 | + static int[] dr = {-1, -1, -1, 0, 1, 1, 1, 0}, dc = {-1, 0, 1, 1, 1, 0, -1, -1}; // 8๋ฐฉ ๋ฐฐ์ด |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + initInput(); |
| 18 | + solution(); |
| 19 | + System.out.println(totalRemoveTreeCnt); |
| 20 | + } |
| 21 | + |
| 22 | + static void solution() { |
| 23 | + |
| 24 | + while(M-- > 0) { |
| 25 | + grow(); // ์ฑ์ฅ |
| 26 | + spread(); // ๋ฒ์ |
| 27 | + remove(); // ์ ์ด์ ๋ฟ๋ฆฌ๊ธฐ |
| 28 | + decreaseRemove(); // ์ ์ด์ ๊ฐ์ |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // ์ ์ด์ ์ง์ ์๊ฐ 1์ฉ ์ค์ด๊ธฐ |
| 33 | + static void decreaseRemove() { |
| 34 | + for(int r = 0; r < remove.length; r++) { |
| 35 | + for(int c = 0; c < remove[0].length; c++) { |
| 36 | + if(remove[r][c] == 0) continue; |
| 37 | + remove[r][c] -= 1; |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + static void remove() { |
| 43 | + // ์ ์ด์ ๊ฐ ๋ฟ๋ ค์ง ์นธ์๋ C๋
๋งํผ ์ ์ด์ ๊ฐ ๋จ์์๋ค๊ฐ C + 1๋
์งธ๊ฐ ๋ ๋ ์ฌ๋ผ์ง |
| 44 | + int removeTreeCnt = -1; |
| 45 | + |
| 46 | + Queue<Integer> spreadPos = new ArrayDeque<Integer>(); // ์ ์ด์ ๊ฐ ํผ์ ธ ๋๊ฐ ์์น |
| 47 | + |
| 48 | + for(int r = 0; r < map.length; r++) { |
| 49 | + for(int c = 0; c < map[0].length; c++) { |
| 50 | + if(map[r][c] == -1) continue; // ๋ฒฝ์ด๋ฉด continue |
| 51 | + |
| 52 | + tmpRemoveTreeCnt = 0; // (r, c)์์ ์ ๊ฑฐํ ์ ์๋ ๋๋ฌด ์ |
| 53 | + |
| 54 | + Queue<Integer> tmp = getRemoveCnt(r, c); |
| 55 | + |
| 56 | + if(tmpRemoveTreeCnt <= removeTreeCnt) continue; |
| 57 | + removeTreeCnt = tmpRemoveTreeCnt; |
| 58 | + |
| 59 | + spreadPos = tmp; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + totalRemoveTreeCnt += removeTreeCnt; |
| 64 | + |
| 65 | + // ์ ์ด์ ๋ฟ๋ ค์ฃผ๊ธฐ |
| 66 | + while(!spreadPos.isEmpty()) { |
| 67 | + int pos = spreadPos.poll(); |
| 68 | + |
| 69 | + int r = pos / N, c = pos % N; |
| 70 | + if(map[r][c] == -1) continue; // ๋ฒฝ์ผ ๊ฒฝ์ฐ ๋์ด๊ฐ์ผ ๋จ!!โจ |
| 71 | + |
| 72 | + map[r][c] = 0; |
| 73 | + remove[r][c] = (C + 1); // ์ ์ด์ ๋ฟ๋ฆฌ๊ธฐ |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + static Queue<Integer> getRemoveCnt(int r, int c) { |
| 78 | + Queue<Integer> spreadPos = new ArrayDeque<>(); |
| 79 | + |
| 80 | + // (r, c)์์ ๋๊ฐ์ 4๋ฐฉ์ผ๋ก K์นธ ๋งํผ ๊ฐ๊ธฐ |
| 81 | + Queue<int[]> q = new ArrayDeque<>(); |
| 82 | + |
| 83 | + int pos = r * N + c; |
| 84 | + |
| 85 | + // (r, c) ์ง์ ์์๋ ๋๊ฐ์ 4๋ฐฉ์ผ๋ก ๋ณด๋ด๊ธฐ |
| 86 | + for(int dir = 0; dir < 8; dir += 2) { |
| 87 | + q.add(new int[] {pos, 0, dir}); |
| 88 | + } |
| 89 | + |
| 90 | + spreadPos.add(pos); // ์ ์ด์ ๊ฐ ํผ์ง๋ ์์น |
| 91 | + tmpRemoveTreeCnt += map[r][c]; |
| 92 | + |
| 93 | + while(!q.isEmpty()) { |
| 94 | + |
| 95 | + int[] current = q.poll(); |
| 96 | + int cr = current[0] / N; |
| 97 | + int cc = current[0] % N; |
| 98 | + int dir = current[2]; |
| 99 | + |
| 100 | + if(map[cr][cc] == 0) continue; // ์ฒ์์ (r, c)๊ฐ ๋น ๊ณต๊ฐ์ผ๋ก ์ฃผ์ด์ก์ ์๋ ์๊ธฐ ๋๋ฌธ์ ์ด๊ฑฐ ์ฒ๋ฆฌํด์ค์ผ ๋จ |
| 101 | + |
| 102 | + int nr = cr + dr[dir]; |
| 103 | + int nc = cc + dc[dir]; |
| 104 | + |
| 105 | + // ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฑฐ๋, ๋๋ฌด๊ฐ ์์ ์๋ ์นธ์ด๊ฑฐ๋, ๋ป์ด๊ฐ ๊ฑฐ๋ฆฌ๊ฐ K๊ฐ ๋์๋ค๋ฉด continue |
| 106 | + if(!check(nr, nc) || current[1] == K) continue; |
| 107 | + |
| 108 | + int nPos = nr * N + nc; |
| 109 | + spreadPos.add(nPos); |
| 110 | + |
| 111 | + // ๋ฒฝ์ด ์๊ฑฐ๋, ๋๋ฌด๊ฐ ์์ ์๋ ์นธ์ ๊ทธ ์นธ ๊น์ง๋ง ์ ์ด์ ๊ฐ ๋ฟ๋ ค์ง |
| 112 | + if(map[nr][nc] <= 0) continue; |
| 113 | + |
| 114 | + q.add(new int[] {nPos, current[1] + 1, dir}); |
| 115 | + tmpRemoveTreeCnt += map[nr][nc]; |
| 116 | + } |
| 117 | + return spreadPos; |
| 118 | + } |
| 119 | + |
| 120 | + // ๋ฒ์ - ๋ฒฝ, ๋ค๋ฅธ ๋๋ฌด, ์ ์ด์ ๋ชจ๋ ์๋ ์นธ์ ๋ฒ์ ์งํ (๋ฒ์์ด ๊ฐ๋ฅํ ์นธ์ ๊ฐ์๋งํผ ๋๋์ด์ง ๊ทธ๋ฃจ ์ ๋งํผ ๋ฒ์) |
| 121 | + static void spread() { |
| 122 | + int[][] tmp = new int[N][N]; |
| 123 | + |
| 124 | + for(int r = 0; r < map.length; r++) { |
| 125 | + for(int c = 0; c < map[0].length; c++) { |
| 126 | + if(map[r][c] <= 0) continue; |
| 127 | + |
| 128 | + int cnt = 0; // ๋ฒ์์ด ๊ฐ๋ฅํ ์นธ์ ๊ฐ์ |
| 129 | + |
| 130 | + for(int d = 1; d < 8; d+= 2) { |
| 131 | + int nr = r + dr[d]; |
| 132 | + int nc = c + dc[d]; |
| 133 | + |
| 134 | + // ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฑฐ๋, ๋ฒฝ & ๋ค๋ฅธ ๋๋ฌด๊ฐ ์๊ฑฐ๋, ์ ์ด์ ๊ฐ ์๋ค๋ฉด continue |
| 135 | + if(!check(nr, nc) || map[nr][nc] != 0 || remove[nr][nc] != 0) continue; |
| 136 | + cnt++; |
| 137 | + } |
| 138 | + |
| 139 | + for(int d = 1; d < 8; d+= 2) { |
| 140 | + int nr = r + dr[d]; |
| 141 | + int nc = c + dc[d]; |
| 142 | + |
| 143 | + // ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฑฐ๋, ๋ฒฝ & ๋ค๋ฅธ ๋๋ฌด๊ฐ ์๊ฑฐ๋, ์ ์ด์ ๊ฐ ์๋ค๋ฉด continue |
| 144 | + if(!check(nr, nc) || map[nr][nc] != 0 || remove[nr][nc] != 0) continue; |
| 145 | + tmp[nr][nc] += map[r][c] / cnt; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + for(int r = 0; r < map.length; r++) { |
| 152 | + for(int c = 0; c < map[0].length; c++) { |
| 153 | + map[r][c] += tmp[r][c]; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // ์ฑ์ฅ - ์ธ์ ํ ๋ค ์นธ์ ์นธ ์ค ๋๋ฌด๊ฐ ์๋ ์๋งํผ ๋๋ฌด ์ฑ์ฅ |
| 159 | + static void grow() { |
| 160 | + |
| 161 | + int[][] tmp = new int[N][N]; // ๋์์ ์ฑ์ฅํด์ผ ๋๋ฏ๋ก, tmp ๋ฐฐ์ด์ ์ฆ๊ฐ์ ๋ํ ์ ๋ณด ์ ์ฅ |
| 162 | + |
| 163 | + for(int r = 0; r < map.length; r++) { |
| 164 | + for(int c = 0; c < map[0].length; c++) { |
| 165 | + if(map[r][c] <= 0) continue; // ๋๋ฌด๊ฐ ์๋๋ผ๋ฉด continue |
| 166 | + |
| 167 | + int tree = 0; |
| 168 | + |
| 169 | + // ์ธ์ ํ ๋ค ์นธ |
| 170 | + for(int d = 1; d < 8; d += 2) { |
| 171 | + |
| 172 | + int nr = r + dr[d]; |
| 173 | + int nc = c + dc[d]; |
| 174 | + |
| 175 | + // ๋ฒ์๋ฅผ ๋ฒ์ด๋๊ฑฐ๋, ๋๋ฌด๊ฐ ์๋ ๋ |
| 176 | + if(!check(nr, nc) || map[nr][nc] <= 0) continue; |
| 177 | + tree += 1; |
| 178 | + } |
| 179 | + |
| 180 | + tmp[r][c] += tree; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + for(int r = 0; r < map.length; r++) { |
| 185 | + for(int c = 0; c < map[0].length; c++) { |
| 186 | + map[r][c] += tmp[r][c]; |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + static boolean check(int r, int c) { |
| 192 | + return r >= 0 && r < map.length && c >= 0 && c < map[0].length; |
| 193 | + } |
| 194 | + |
| 195 | + static void initInput() throws Exception { |
| 196 | + System.setIn(new FileInputStream("./input/๋๋ฌด๋ฐ๋ฉธ.txt")); |
| 197 | + |
| 198 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 199 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 200 | + |
| 201 | + N = Integer.parseInt(st.nextToken()); // ๊ฒฉ์์ ํฌ๊ธฐ |
| 202 | + M = Integer.parseInt(st.nextToken()); // ๋ฐ๋ฉธ์ด ์งํ๋๋ ๋
์ |
| 203 | + K = Integer.parseInt(st.nextToken()); // ์ ์ด์ ์ ํ์ฐ ๋ฒ์ |
| 204 | + C = Integer.parseInt(st.nextToken()); // ์ ์ด์ ๊ฐ ๋จ์์๋ ๋
์ |
| 205 | + |
| 206 | + map = new int[N][N]; // ๋๋ฌด |
| 207 | + remove = new int[N][N]; // ์ ์ด์ |
| 208 | + |
| 209 | + for(int r = 0; r < map.length; r++) { |
| 210 | + st = new StringTokenizer(br.readLine()); |
| 211 | + |
| 212 | + for(int c = 0; c < map[0].length; c++) map[r][c] = Integer.parseInt(st.nextToken()); |
| 213 | + } |
| 214 | + } |
| 215 | +} |
0 commit comments