|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static class Shark { |
| 8 | + int r, c, s, d, z; |
| 9 | + boolean alive; |
| 10 | + |
| 11 | + Shark(int r, int c, int s, int d, int z) { |
| 12 | + this.r = r; this.c = c; this.s = s; this.d = d; this.z = z; |
| 13 | + this.alive = true; |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + static int R, C, M; |
| 18 | + static Shark[] sharks; |
| 19 | + static int[][] board; |
| 20 | + static final int[] dr = {0, -1, 1, 0, 0}; |
| 21 | + static final int[] dc = {0, 0, 0, 1, -1}; |
| 22 | + |
| 23 | + public static void main(String[] args) throws Exception { |
| 24 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 25 | + StringTokenizer st; |
| 26 | + |
| 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[M]; |
| 33 | + board = new int[R + 1][C + 1]; |
| 34 | + for (int i = 1; i <= R; i++) Arrays.fill(board[i], -1); |
| 35 | + |
| 36 | + for (int i = 0; i < M; i++) { |
| 37 | + st = new StringTokenizer(br.readLine()); |
| 38 | + int r = Integer.parseInt(st.nextToken()); |
| 39 | + int c = Integer.parseInt(st.nextToken()); |
| 40 | + int s = Integer.parseInt(st.nextToken()); |
| 41 | + int d = Integer.parseInt(st.nextToken()); |
| 42 | + int z = Integer.parseInt(st.nextToken()); |
| 43 | + sharks[i] = new Shark(r, c, s, d, z); |
| 44 | + } |
| 45 | + |
| 46 | + for (int i = 0; i < M; i++) { |
| 47 | + if (sharks[i].alive) board[sharks[i].r][sharks[i].c] = i; |
| 48 | + } |
| 49 | + |
| 50 | + int answer = 0; |
| 51 | + |
| 52 | + for (int col = 1; col <= C; col++) { |
| 53 | + for (int row = 1; row <= R; row++) { |
| 54 | + int idx = board[row][col]; |
| 55 | + if (idx != -1 && sharks[idx].alive) { |
| 56 | + sharks[idx].alive = false; |
| 57 | + answer += sharks[idx].z; |
| 58 | + board[row][col] = -1; |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + moveAll(); |
| 64 | + |
| 65 | + resolveConflicts(); |
| 66 | + } |
| 67 | + |
| 68 | + System.out.println(answer); |
| 69 | + } |
| 70 | + |
| 71 | + static void moveAll() { |
| 72 | + for (int i = 1; i <= R; i++) Arrays.fill(board[i], -1); |
| 73 | + |
| 74 | + for (int i = 0; i < M; i++) { |
| 75 | + Shark s = sharks[i]; |
| 76 | + if (!s.alive) continue; |
| 77 | + |
| 78 | + int nr = s.r, nc = s.c, nd = s.d, rem = s.s; |
| 79 | + while (rem-- > 0) { |
| 80 | + int tr = nr + dr[nd]; |
| 81 | + int tc = nc + dc[nd]; |
| 82 | + |
| 83 | + if (tr < 1 || tr > R || tc < 1 || tc > C) { |
| 84 | + if (nd == 1) nd = 2; |
| 85 | + else if (nd == 2) nd = 1; |
| 86 | + else if (nd == 3) nd = 4; |
| 87 | + else nd = 3; |
| 88 | + tr = nr + dr[nd]; |
| 89 | + tc = nc + dc[nd]; |
| 90 | + } |
| 91 | + |
| 92 | + nr = tr; nc = tc; |
| 93 | + } |
| 94 | + |
| 95 | + s.r = nr; s.c = nc; s.d = nd; |
| 96 | + |
| 97 | + if (board[nr][nc] == -1) board[nr][nc] = i; |
| 98 | + else { |
| 99 | + board[nr][nc] = -2; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + static void resolveConflicts() { |
| 105 | + int[][] newBoard = new int[R + 1][C + 1]; |
| 106 | + for (int i = 1; i <= R; i++) Arrays.fill(newBoard[i], -1); |
| 107 | + |
| 108 | + for (int i = 0; i < M; i++) { |
| 109 | + Shark s = sharks[i]; |
| 110 | + if (!s.alive) continue; |
| 111 | + int r = s.r, c = s.c; |
| 112 | + |
| 113 | + int curIdx = newBoard[r][c]; |
| 114 | + if (curIdx == -1) { |
| 115 | + newBoard[r][c] = i; |
| 116 | + } else { |
| 117 | + if (sharks[curIdx].z < s.z) { |
| 118 | + sharks[curIdx].alive = false; |
| 119 | + newBoard[r][c] = i; |
| 120 | + } else { |
| 121 | + s.alive = false; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + board = newBoard; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +``` |
0 commit comments