|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +public class Main{ |
| 6 | + static class Shark{ |
| 7 | + int speed; |
| 8 | + int direction; |
| 9 | + int size; |
| 10 | + |
| 11 | + Shark(int speed, int direction, int size){ |
| 12 | + this.speed = speed; |
| 13 | + this.direction = direction; |
| 14 | + this.size = size; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 19 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 20 | + static StringTokenizer st; |
| 21 | + |
| 22 | + static int R, C, M; |
| 23 | + static Shark[][] sharks; |
| 24 | + static int ans = 0; |
| 25 | + |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + st = new StringTokenizer(br.readLine()); |
| 28 | + R = Integer.parseInt(st.nextToken()); |
| 29 | + C = Integer.parseInt(st.nextToken()); |
| 30 | + M = Integer.parseInt(st.nextToken()); |
| 31 | + |
| 32 | + sharks = new Shark[R+1][C+1]; |
| 33 | + |
| 34 | + for (int i = 0; i < M; i++) { |
| 35 | + st = new StringTokenizer(br.readLine()); |
| 36 | + int r = Integer.parseInt(st.nextToken()); |
| 37 | + int c = Integer.parseInt(st.nextToken()); |
| 38 | + int s = Integer.parseInt(st.nextToken()); |
| 39 | + int d = Integer.parseInt(st.nextToken()); |
| 40 | + int z = Integer.parseInt(st.nextToken()); |
| 41 | + |
| 42 | + sharks[r][c] = new Shark(s, d, z); |
| 43 | + } |
| 44 | + |
| 45 | + // 낚시왕이 오른쪽으로 한 칸씩 이동하면서 상어 잡기 |
| 46 | + for (int col = 1; col <= C; col++) { |
| 47 | + ans += catchShark(col); |
| 48 | + sharkMove(); |
| 49 | + } |
| 50 | + |
| 51 | + bw.write(ans + "\n"); |
| 52 | + bw.flush(); |
| 53 | + bw.close(); |
| 54 | + } |
| 55 | + |
| 56 | + static int catchShark(int col){ |
| 57 | + for (int i = 1; i <= R; i++) { |
| 58 | + if(sharks[i][col] != null){ |
| 59 | + int size = sharks[i][col].size; |
| 60 | + sharks[i][col] = null; |
| 61 | + return size; |
| 62 | + } |
| 63 | + } |
| 64 | + return 0; |
| 65 | + } |
| 66 | + |
| 67 | + static void sharkMove(){ |
| 68 | + Shark[][] newSharks = new Shark[R+1][C+1]; |
| 69 | + |
| 70 | + for (int i = 1; i <= R; i++) { |
| 71 | + for (int j = 1; j <= C; j++) { |
| 72 | + if(sharks[i][j] != null){ |
| 73 | + Shark shark = sharks[i][j]; |
| 74 | + int[] next = nextState(i, j, shark.speed, shark.direction); |
| 75 | + int ni = next[0]; |
| 76 | + int nj = next[1]; |
| 77 | + int nd = next[2]; |
| 78 | + |
| 79 | + if(newSharks[ni][nj] == null || newSharks[ni][nj].size < shark.size){ |
| 80 | + newSharks[ni][nj] = new Shark(shark.speed, nd, shark.size); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + sharks = newSharks; |
| 86 | + } |
| 87 | + |
| 88 | + static int[] nextState(int row, int col, int speed, int direction){ |
| 89 | + int nextRow = row; |
| 90 | + int nextCol = col; |
| 91 | + int nextDirection = direction; |
| 92 | + |
| 93 | + if(direction == 1){ // 위 |
| 94 | + int restShift = speed % ((R-1) * 2); |
| 95 | + if(restShift >= nextRow - 1){ |
| 96 | + restShift -= (nextRow - 1); |
| 97 | + nextDirection = 2; |
| 98 | + nextRow = 1; |
| 99 | + |
| 100 | + if(restShift >= R - 1){ |
| 101 | + restShift -= (R - 1); |
| 102 | + nextDirection = 1; |
| 103 | + nextRow = R; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if(nextDirection == 1){ |
| 108 | + nextRow -= restShift; |
| 109 | + } else { |
| 110 | + nextRow += restShift; |
| 111 | + } |
| 112 | + |
| 113 | + } else if(direction == 2){ // 아래 |
| 114 | + int restShift = speed % ((R-1) * 2); |
| 115 | + if(restShift >= R - nextRow){ |
| 116 | + restShift -= (R - nextRow); |
| 117 | + nextDirection = 1; |
| 118 | + nextRow = R; |
| 119 | + |
| 120 | + if(restShift >= R - 1){ |
| 121 | + restShift -= (R - 1); |
| 122 | + nextDirection = 2; |
| 123 | + nextRow = 1; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if(nextDirection == 1){ |
| 128 | + nextRow -= restShift; |
| 129 | + } else { |
| 130 | + nextRow += restShift; |
| 131 | + } |
| 132 | + |
| 133 | + } else if(direction == 3){ // 오른쪽 |
| 134 | + int restShift = speed % ((C-1) * 2); |
| 135 | + if(restShift >= C - nextCol){ |
| 136 | + restShift -= (C - nextCol); |
| 137 | + nextDirection = 4; |
| 138 | + nextCol = C; |
| 139 | + |
| 140 | + if(restShift >= C - 1){ |
| 141 | + restShift -= (C - 1); |
| 142 | + nextDirection = 3; |
| 143 | + nextCol = 1; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if(nextDirection == 3){ |
| 148 | + nextCol += restShift; |
| 149 | + } else { |
| 150 | + nextCol -= restShift; |
| 151 | + } |
| 152 | + |
| 153 | + } else if(direction == 4){ // 왼쪽 |
| 154 | + int restShift = speed % ((C-1) * 2); |
| 155 | + if(restShift >= nextCol - 1){ |
| 156 | + restShift -= (nextCol - 1); |
| 157 | + nextDirection = 3; |
| 158 | + nextCol = 1; |
| 159 | + |
| 160 | + if(restShift >= C - 1){ |
| 161 | + restShift -= (C - 1); |
| 162 | + nextDirection = 4; |
| 163 | + nextCol = C; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if(nextDirection == 3){ |
| 168 | + nextCol += restShift; |
| 169 | + } else { |
| 170 | + nextCol -= restShift; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return new int[]{nextRow, nextCol, nextDirection}; |
| 175 | + } |
| 176 | +} |
| 177 | +``` |
0 commit comments