|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class DH_생명과학부_랩_인턴 { |
| 5 | + static class Node { |
| 6 | + int r, c, s, d, b; |
| 7 | + boolean isDie; |
| 8 | + |
| 9 | + public Node(int r, int c, int s, int d, int b) { |
| 10 | + this.r = r; |
| 11 | + this.c = c; |
| 12 | + this.s = s; // 속력 |
| 13 | + this.d = d; // 이동 방향 |
| 14 | + this.b = b; // 곰팡이의 크기 |
| 15 | + } |
| 16 | + |
| 17 | + public void update(int r, int c, int d) { |
| 18 | + this.r = r; |
| 19 | + this.c = c; |
| 20 | + this.d = d; |
| 21 | + } |
| 22 | + } |
| 23 | + static int[] dr = {0, -1, 1, 0}, dc = {-1, 0, 0, 1}; // 좌, 상, 하, 우 |
| 24 | + static int[][] map; // 곰팡이의 idx 저장 |
| 25 | + static int N, M, K, totalCnt; |
| 26 | + static ArrayList<Node> list; |
| 27 | + |
| 28 | + public static void main(String[] args) throws Exception { |
| 29 | + initInput(); |
| 30 | + solution(); |
| 31 | + } |
| 32 | + |
| 33 | + static void solution() { |
| 34 | + for(int c = 0; c < map[0].length; c++) { // 1. 첫 번쨰 열부터 탐색 |
| 35 | + step1(c); // 곰팡이 채취 |
| 36 | + step2(); // 곰팡이 이동 |
| 37 | + } |
| 38 | + |
| 39 | + System.out.println(totalCnt); |
| 40 | + } |
| 41 | + |
| 42 | + static void printMap() { |
| 43 | + for(int r = 0; r < map.length; r++) { |
| 44 | + System.out.println(Arrays.toString(map[r])); |
| 45 | + } |
| 46 | + |
| 47 | + System.out.println(); |
| 48 | + } |
| 49 | + |
| 50 | + static void step2() { |
| 51 | + map = new int[N][M]; |
| 52 | + |
| 53 | + for(int i = 0; i < list.size(); i++) { |
| 54 | + if(list.get(i).isDie) continue; |
| 55 | + |
| 56 | + int r = list.get(i).r, c = list.get(i).c; |
| 57 | + int s = list.get(i).s, d = list.get(i).d; |
| 58 | + |
| 59 | + int mod = 0; |
| 60 | + if(d == 1 || d == 2) mod = N * 2 - 2; // 상, 하로 움직이는 경우 행의 크기 * 2 - 2 |
| 61 | + else mod = M * 2 - 2; |
| 62 | + s %= mod; |
| 63 | + |
| 64 | + for(int dis = 0; dis < s; dis++) { |
| 65 | + int nr = r + dr[d]; |
| 66 | + int nc = c + dc[d]; |
| 67 | + |
| 68 | + if(!check(nr, nc)) { |
| 69 | + dis -= 1; |
| 70 | + d = changeDir(d); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + r = nr; |
| 75 | + c = nc; |
| 76 | + } |
| 77 | + |
| 78 | + list.get(i).update(r, c, d); |
| 79 | + |
| 80 | + if(map[r][c] == 0) { |
| 81 | + map[r][c] = i + 1; |
| 82 | + } else if(map[r][c] < i + 1) { |
| 83 | + int nextIdx = map[r][c]; |
| 84 | + if(list.get(nextIdx - 1).b < list.get(i).b) { |
| 85 | + list.get(nextIdx - 1).isDie = true; |
| 86 | + map[r][c] = i + 1; |
| 87 | + } else { |
| 88 | + list.get(i).isDie = true; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + static boolean check(int r, int c) { |
| 95 | + return r >= 0 && r < N && c >= 0 && c < M; |
| 96 | + } |
| 97 | + static int changeDir(int d) { |
| 98 | + if(d == 0) return 3; |
| 99 | + if(d == 3) return 0; |
| 100 | + if(d == 1) return 2; |
| 101 | + return 1; |
| 102 | + } |
| 103 | + // 곰팡이 채취 |
| 104 | + static void step1(int c) { |
| 105 | + for(int r = 0; r < map.length; r++) { |
| 106 | + if(map[r][c] == 0) continue; |
| 107 | + int idx = map[r][c] - 1; |
| 108 | + totalCnt += list.get(idx).b; |
| 109 | + list.get(idx).isDie = true; |
| 110 | + map[r][c] = 0; |
| 111 | + return; |
| 112 | + } |
| 113 | + } |
| 114 | + static void initInput() throws Exception { |
| 115 | + System.setIn(new FileInputStream("./input/생명과학부 랩 인턴.txt")); |
| 116 | + |
| 117 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 118 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 119 | + |
| 120 | + N = Integer.parseInt(st.nextToken()); // 격자의 크기 |
| 121 | + M = Integer.parseInt(st.nextToken()); // 격자의 크기 |
| 122 | + K = Integer.parseInt(st.nextToken()); // 곰팡이의 개수 |
| 123 | + |
| 124 | + map = new int[N][M]; |
| 125 | + list = new ArrayList<>(); |
| 126 | + |
| 127 | + for(int i = 0; i < K; i++) { |
| 128 | + st = new StringTokenizer(br.readLine()); |
| 129 | + |
| 130 | + int r = Integer.parseInt(st.nextToken()) - 1; |
| 131 | + int c = Integer.parseInt(st.nextToken()) - 1; |
| 132 | + int s = Integer.parseInt(st.nextToken()); |
| 133 | + int d = Integer.parseInt(st.nextToken()) % 4; |
| 134 | + int b = Integer.parseInt(st.nextToken()); |
| 135 | + |
| 136 | + list.add(new Node(r, c, s, d, b)); |
| 137 | + map[r][c] = i + 1; // 곰팡이의 idx에 + 1을 해주고 map에 넣어줌 |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments