|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.ArrayDeque; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.LinkedList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Queue; |
| 14 | +import java.util.Set; |
| 15 | +import java.util.TreeSet; |
| 16 | + |
| 17 | +public class Main{ |
| 18 | + // n*n 땅, r c 는 1부터 |
| 19 | + // 처음은 모든칸에 양분 5 |
| 20 | + // M개의 나무 땅에 |
| 21 | + // 한칸에 여러개의 나무 가능 |
| 22 | + private static class Tree implements Comparable<Tree>{ |
| 23 | + int year; |
| 24 | + Tree(int year){ |
| 25 | + this.year = year; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public int compareTo(Tree o){ |
| 30 | + return this.year - o.year; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + private static int[][] yangbuns; |
| 35 | + private static int[][] yangbunDelta; |
| 36 | + private static int n; |
| 37 | + private static int m; |
| 38 | + private static int k; |
| 39 | + private static Map<Integer, List<Tree>> treesAt1D; |
| 40 | + public static void main(String[] args) throws IOException { |
| 41 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 42 | + String[] tokens = br.readLine().split(" "); |
| 43 | + n = Integer.parseInt(tokens[0] ); |
| 44 | + m = Integer.parseInt(tokens[1] ); |
| 45 | + k = Integer.parseInt(tokens[2] ); |
| 46 | + yangbuns = new int[n][n]; |
| 47 | + yangbunDelta = new int[n][n]; |
| 48 | + for(int r = 0; r<n;r++) |
| 49 | + for(int c = 0; c<n;c++) |
| 50 | + yangbuns[r][c] = 5; |
| 51 | + int t = n; |
| 52 | + treesAt1D = new HashMap<>(); |
| 53 | + for(int r = 0; r<n; r++){ |
| 54 | + tokens = br.readLine().split(" "); |
| 55 | + for(int c = 0; c<n;c++){ |
| 56 | + yangbunDelta[r][c] = Integer.parseInt(tokens[c]); |
| 57 | + treesAt1D.putIfAbsent(convert2to1(r, c), new LinkedList<Tree>()); |
| 58 | + } |
| 59 | + } |
| 60 | + for(int i = 0; i<m;i++){ |
| 61 | + tokens = br.readLine().split(" "); |
| 62 | + int r = Integer.parseInt(tokens[0]) - 1; |
| 63 | + int c = Integer.parseInt(tokens[1]) - 1; |
| 64 | + int year = Integer.parseInt(tokens[2]); |
| 65 | + // treesAt1D.putIfAbsent(convert2to1(r, c), new TreeSet<Tree>()); |
| 66 | + treesAt1D.get(convert2to1(r, c)).add(new Tree(year)); |
| 67 | + } |
| 68 | + |
| 69 | + for(Map.Entry<Integer, List<Tree>> entry : treesAt1D.entrySet()){ |
| 70 | + int coord1D = entry.getKey(); |
| 71 | + int[] temp = convert1to2(coord1D); |
| 72 | + int r = temp[0]; int c = temp[1]; |
| 73 | + List<Tree> treeQ = entry.getValue(); |
| 74 | + treeQ.sort((o1, o2) -> o1.year - o2.year); |
| 75 | + } |
| 76 | + |
| 77 | + int kk = k; |
| 78 | + while(kk-->0){ |
| 79 | + // 봄-> 자신의 나이만큼 양분 먹음->나이++, 여러개나무-> 나이가 어린 나무부터 먹음 |
| 80 | + Queue<int[]> yangbunsForSummer = new ArrayDeque<int[]>(); |
| 81 | + Queue<int[]> yangbunsForFall = new ArrayDeque<int[]>(); |
| 82 | + for(Map.Entry<Integer, List<Tree>> entry : treesAt1D.entrySet()){ |
| 83 | + int coord1D = entry.getKey(); |
| 84 | + int[] temp = convert1to2(coord1D); |
| 85 | + int r = temp[0]; int c = temp[1]; |
| 86 | + List<Tree> treeQ = entry.getValue(); |
| 87 | + Iterator<Tree> iter = treeQ.iterator(); |
| 88 | + boolean shouldRemove = false; |
| 89 | + Tree tree = null; |
| 90 | + while(iter.hasNext()){ |
| 91 | + tree = iter.next(); |
| 92 | + if(shouldRemove){ |
| 93 | + yangbunsForSummer.add(new int[]{r, c, tree.year/2}); |
| 94 | + iter.remove(); |
| 95 | + }else{ |
| 96 | + yangbuns[r][c]-= tree.year; |
| 97 | + if(yangbuns[r][c]<0){ |
| 98 | + // 먹었더니 양분 값이 음수 |
| 99 | + //원상복귀 |
| 100 | + yangbuns[r][c]+= tree.year; |
| 101 | + shouldRemove = true; |
| 102 | + yangbunsForSummer.add(new int[]{r, c, tree.year/2}); |
| 103 | + iter.remove(); |
| 104 | + }else{ |
| 105 | + tree.year++; |
| 106 | + if(tree.year % 5 == 0){ |
| 107 | + yangbunsForFall.add(new int[]{r, c}); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + // 만약 양분 부족-> 나무 바로죽음 |
| 114 | + // 여름-> 봄에 죽은 나무-> 양분, 죽은 나무 나이/2 값이 추가 |
| 115 | + while(!yangbunsForSummer.isEmpty()){ |
| 116 | + int[] yangbunInfo = yangbunsForSummer.poll(); |
| 117 | + int r = yangbunInfo[0]; int c = yangbunInfo[1]; |
| 118 | + yangbuns[r][c]+= yangbunInfo[2]; |
| 119 | + } |
| 120 | + // 가을-> 나이 5의배수인 나무가 번식 -> 인접한 8개칸에 나이1인 나무생김(바운드고려) |
| 121 | + for(int[] coords: yangbunsForFall){ |
| 122 | + int r = coords[0]; |
| 123 | + int c = coords[1]; |
| 124 | + for(int dr = -1; dr <=1; dr++){ |
| 125 | + for(int dc = -1; dc <=1; dc++){ |
| 126 | + if(dr == 0 && dc == 0) continue; |
| 127 | + int nr = r+dr; |
| 128 | + int nc = c+dc; |
| 129 | + if(nr < 0 || nr >= n || nc < 0 || nc >= n) continue; |
| 130 | + treesAt1D.get(convert2to1(nr, nc)).add(0, new Tree(1)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + // 겨울-> 입력으로 해당 땅에 양분추가됨 |
| 135 | + for(int r = 0; r<n; r++) |
| 136 | + for(int c = 0; c<n; c++){ |
| 137 | + yangbuns[r][c] += yangbunDelta[r][c]; |
| 138 | + } |
| 139 | + // K년이 지난후 살아있는 나무개수 |
| 140 | + } |
| 141 | + int answer =0; |
| 142 | + for(Map.Entry<Integer, List<Tree>> entry : treesAt1D.entrySet()){ |
| 143 | + answer += entry.getValue().size(); |
| 144 | + } |
| 145 | + System.out.println(answer); |
| 146 | + br.close(); |
| 147 | + } |
| 148 | + |
| 149 | + private static int[] convert1to2(int coord1D) { |
| 150 | + return new int[]{coord1D / n, coord1D % n}; |
| 151 | + } |
| 152 | + |
| 153 | + private static Integer convert2to1(int r, int c) { |
| 154 | + return r*n + c; |
| 155 | + } |
| 156 | +} |
| 157 | +``` |
0 commit comments