|
| 1 | +package BOJ.Day0924.BOJ나무_재테크; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class BOJ16235 { |
| 7 | + static int N, M, K; |
| 8 | + static int[][] grid, A; // 땅, 추가양분 |
| 9 | + static ArrayList<Tree> plant = new ArrayList<>(); |
| 10 | + static Queue<Integer> deadTree = new LinkedList<>(); |
| 11 | + static int[] dr = {-1, 1, 0, 0, -1, -1, 1, 1, 1}; // 상 하 좌 우 우상 좌상 우하 좌하 |
| 12 | + static int[] dc = {0, 0, -1, 1, -1, 1, -1, 1}; |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 16 | + N = Integer.parseInt(st.nextToken()); |
| 17 | + M = Integer.parseInt(st.nextToken()); |
| 18 | + K = Integer.parseInt(st.nextToken()); |
| 19 | + grid = new int[N][N]; |
| 20 | + A = new int[N][N]; |
| 21 | + |
| 22 | + for (int i = 0; i < N; i++) { |
| 23 | + st = new StringTokenizer(br.readLine()); |
| 24 | + for (int j = 0; j < N; j++) { |
| 25 | + A[i][j] = Integer.parseInt(st.nextToken()); |
| 26 | + grid[i][j] = 5; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + for (int i = 0; i < M; i++) { |
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + int x = Integer.parseInt(st.nextToken()) - 1; |
| 33 | + int y = Integer.parseInt(st.nextToken()) - 1; |
| 34 | + int z = Integer.parseInt(st.nextToken()); |
| 35 | + plant.add(new Tree(x, y, z)); |
| 36 | + } |
| 37 | + Collections.sort(plant, (o1, o2) -> o1.age - o2.age); |
| 38 | + |
| 39 | + for (int i = 0; i < K; i++) { |
| 40 | + spring(); |
| 41 | + summer(); |
| 42 | + fall(); |
| 43 | + winter(); |
| 44 | + Collections.sort(plant, (o1, o2) -> o1.age - o2.age); |
| 45 | + } |
| 46 | + System.out.println(plant.size()); |
| 47 | + } |
| 48 | + public static void spring() { |
| 49 | + for (int i = 0; i < plant.size(); i++) { |
| 50 | + Tree tree = plant.get(i); |
| 51 | + if (grid[tree.x][tree.y] < tree.age) { |
| 52 | + deadTree.add(i); |
| 53 | + } |
| 54 | + else { |
| 55 | + grid[tree.x][tree.y] -= tree.age; |
| 56 | + tree.age++; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + public static void summer() { |
| 61 | + while (!deadTree.isEmpty()) { |
| 62 | + Tree tree = plant.get(deadTree.poll()); |
| 63 | + grid[tree.x][tree.y] += (tree.age / 2); |
| 64 | + tree.deadTree = true; |
| 65 | + } |
| 66 | + } |
| 67 | + public static void fall() { |
| 68 | + for (int i = 0; i < plant.size(); i++){ |
| 69 | + Tree tree = plant.get(i); |
| 70 | + if (tree.deadTree) continue; |
| 71 | + if (tree.age % 5 == 0) { |
| 72 | + for (int d = 0; d < 8; d++) { |
| 73 | + int nr = tree.x + dr[d]; |
| 74 | + int nc = tree.y + dc[d]; |
| 75 | + if (nr < 0 || nc < 0 || nr >= N || nc >= N) continue; |
| 76 | + plant.add(new Tree(nr, nc, 1)); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + ArrayList<Tree> newTree = new ArrayList<>(); |
| 81 | + for (Tree tree : plant) { |
| 82 | + if (tree.deadTree) continue; |
| 83 | + newTree.add(tree); |
| 84 | + } |
| 85 | + plant = newTree; |
| 86 | + } |
| 87 | + public static void winter() { |
| 88 | + for (int i = 0; i < N; i++) { |
| 89 | + for (int j = 0; j < N; j++) { |
| 90 | + grid[i][j] += A[i][j]; |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class Tree { |
| 97 | + int x, y, age; |
| 98 | + boolean deadTree; |
| 99 | + Tree(int x, int y, int age) { |
| 100 | + this.x = x; |
| 101 | + this.y = y; |
| 102 | + this.age = age; |
| 103 | + } |
| 104 | +} |
0 commit comments