|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static int r, c, t; |
| 7 | + static int[][] map; |
| 8 | + static int up, down; |
| 9 | + static final int[] dy = {-1, 1, 0, 0}; |
| 10 | + static final int[] dx = {0, 0, -1, 1}; |
| 11 | + |
| 12 | + public static void main(String[] args) throws Exception { |
| 13 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 14 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 15 | + r = Integer.parseInt(st.nextToken()); |
| 16 | + c = Integer.parseInt(st.nextToken()); |
| 17 | + t = Integer.parseInt(st.nextToken()); |
| 18 | + |
| 19 | + map = new int[r][c]; |
| 20 | + up = down = -1; |
| 21 | + |
| 22 | + for (int i = 0; i < r; i++) { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + for (int j = 0; j < c; j++) { |
| 25 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 26 | + if (map[i][j] == -1) { |
| 27 | + if (up == -1) up = i; |
| 28 | + else down = i; |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + while (t-- > 0) { |
| 34 | + spread(); |
| 35 | + purify(); |
| 36 | + } |
| 37 | + |
| 38 | + long sum = 0; |
| 39 | + for (int i = 0; i < r; i++) |
| 40 | + for (int j = 0; j < c; j++) |
| 41 | + if (map[i][j] > 0) sum += map[i][j]; |
| 42 | + |
| 43 | + System.out.println(sum); |
| 44 | + } |
| 45 | + |
| 46 | + static void spread() { |
| 47 | + int[][] add = new int[r][c]; |
| 48 | + for (int y = 0; y < r; y++) { |
| 49 | + for (int x = 0; x < c; x++) { |
| 50 | + if (map[y][x] <= 0) continue; |
| 51 | + int amount = map[y][x] / 5; |
| 52 | + if (amount == 0) continue; |
| 53 | + |
| 54 | + int cnt = 0; |
| 55 | + for (int d = 0; d < 4; d++) { |
| 56 | + int ny = y + dy[d], nx = x + dx[d]; |
| 57 | + if (ny < 0 || nx < 0 || ny >= r || nx >= c) continue; |
| 58 | + if (map[ny][nx] == -1) continue; |
| 59 | + add[ny][nx] += amount; |
| 60 | + cnt++; |
| 61 | + } |
| 62 | + map[y][x] -= amount * cnt; |
| 63 | + } |
| 64 | + } |
| 65 | + for (int i = 0; i < r; i++) { |
| 66 | + for (int j = 0; j < c; j++) { |
| 67 | + if (map[i][j] == -1) continue; |
| 68 | + map[i][j] += add[i][j]; |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static void purify() { |
| 74 | + for (int i = up - 1; i > 0; i--) map[i][0] = map[i - 1][0]; |
| 75 | + for (int j = 0; j < c - 1; j++) map[0][j] = map[0][j + 1]; |
| 76 | + for (int i = 0; i < up; i++) map[i][c - 1] = map[i + 1][c - 1]; |
| 77 | + for (int j = c - 1; j > 1; j--) map[up][j] = map[up][j - 1]; |
| 78 | + map[up][1] = 0; |
| 79 | + map[up][0] = -1; |
| 80 | + |
| 81 | + for (int i = down + 1; i < r - 1; i++) map[i][0] = map[i + 1][0]; |
| 82 | + for (int j = 0; j < c - 1; j++) map[r - 1][j] = map[r - 1][j + 1]; |
| 83 | + for (int i = r - 1; i > down; i--) map[i][c - 1] = map[i - 1][c - 1]; |
| 84 | + for (int j = c - 1; j > 1; j--) map[down][j] = map[down][j - 1]; |
| 85 | + map[down][1] = 0; |
| 86 | + map[down][0] = -1; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +``` |
0 commit comments