|
| 1 | +# 5:06~ |
| 2 | + |
| 3 | +from heapq import heappush, heappop |
| 4 | + |
| 5 | +N = M = P = C = D = 0 |
| 6 | +santa_state = [] |
| 7 | +score = [] |
| 8 | +santas = [] |
| 9 | +board = [] |
| 10 | +out_count = 0 |
| 11 | + |
| 12 | + |
| 13 | +def solution(): |
| 14 | + global N, M, P, C, D, santa_state, score, santas, board, out_count |
| 15 | + N, M, P, C, D = map(int, input().split()) |
| 16 | + r, c = map(lambda v: int(v) - 1, input().split()) |
| 17 | + board = [[0 for _ in range(N)] for _ in range(N)] |
| 18 | + santas = [[0, 0] for _ in range(P)] |
| 19 | + santa_state = [0 for _ in range(P)] |
| 20 | + score = [0 for _ in range(P)] |
| 21 | + |
| 22 | + board[r][c] = -1 |
| 23 | + for i in range(P): |
| 24 | + num, sr, sc = map(int, input().split()) |
| 25 | + board[sr - 1][sc - 1] = num |
| 26 | + santas[num - 1] = [sr - 1, sc - 1] |
| 27 | + |
| 28 | + for t in range(M): |
| 29 | + |
| 30 | + # # 루돌프 턴 |
| 31 | + rudolf_nr, rudolf_nc, r_dir = moveR(r, c) |
| 32 | + n_num = board[rudolf_nr][rudolf_nc] |
| 33 | + board[r][c] = 0 |
| 34 | + r = rudolf_nr |
| 35 | + c = rudolf_nc |
| 36 | + board[r][c] = -1 |
| 37 | + |
| 38 | + if n_num > 0: |
| 39 | + nr, nc = crashByRudolf(rudolf_nr, rudolf_nc, r_dir) |
| 40 | + score[n_num - 1] += C |
| 41 | + if not (0 <= nr < N and 0 <= nc < N): |
| 42 | + santa_state[n_num - 1] = -1 |
| 43 | + out_count += 1 |
| 44 | + else: |
| 45 | + santa_state[n_num - 1] = 2 |
| 46 | + temp = board[nr][nc] |
| 47 | + setSantaLocation(nr, nc, n_num) |
| 48 | + |
| 49 | + if temp > 0: |
| 50 | + crashSantas(nr, nc, temp, r_dir) |
| 51 | + # 산타턴 |
| 52 | + for idx, santa in enumerate(santas): |
| 53 | + if 0 < santa_state[idx]: # faint |
| 54 | + # santa_state[idx] -= 1 |
| 55 | + continue |
| 56 | + elif santa_state[idx] == -1: # out |
| 57 | + continue |
| 58 | + |
| 59 | + santa_num = idx + 1 |
| 60 | + sr, sc, s_dir = moveS(santa, (r, c)) |
| 61 | + |
| 62 | + board[santa[0]][santa[1]] = 0 |
| 63 | + |
| 64 | + if not (0 <= sr < N and 0 <= sc < N): # out |
| 65 | + santa_state[idx] = -1 |
| 66 | + out_count += 1 |
| 67 | + else: |
| 68 | + temp = board[sr][sc] |
| 69 | + |
| 70 | + if temp == -1: # 루돌프랑 부딪힘 |
| 71 | + santa_state[idx] = 2 |
| 72 | + score[idx] += D |
| 73 | + nr, nc = crashBySanta(sr, sc, s_dir) |
| 74 | + if not (0 <= nr < N and 0 <= nc < N): |
| 75 | + santa_state[idx] = -1 |
| 76 | + out_count += 1 |
| 77 | + else: |
| 78 | + santa_state[idx] = 2 |
| 79 | + temp = board[nr][nc] |
| 80 | + setSantaLocation(nr, nc, santa_num) |
| 81 | + |
| 82 | + if temp > 0: |
| 83 | + crashSantas(nr, nc, temp, [-d for d in s_dir]) |
| 84 | + else: |
| 85 | + setSantaLocation(sr, sc, santa_num) |
| 86 | + |
| 87 | + # 턴별 점수 계산 |
| 88 | + for i in range(P): |
| 89 | + if santa_state[i] == -1: |
| 90 | + continue |
| 91 | + if 0 < santa_state[i]: # faint |
| 92 | + santa_state[i] -= 1 |
| 93 | + score[i] += 1 |
| 94 | + |
| 95 | + if out_count == P: |
| 96 | + return score |
| 97 | + |
| 98 | + return score |
| 99 | + |
| 100 | + |
| 101 | +def setSantaLocation(r, c, num): |
| 102 | + board[r][c] = num |
| 103 | + santas[num - 1] = (r, c) |
| 104 | + |
| 105 | + |
| 106 | +def moveR(r, c): |
| 107 | + pq = [] |
| 108 | + # 가장 가까운 산타 찾기 |
| 109 | + # rd = ((1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)) |
| 110 | + for i in range(N): |
| 111 | + for j in range(N): |
| 112 | + if board[i][j] > 0: |
| 113 | + heappush(pq, ((i - r) ** 2 + (j - c) ** 2, -i, -j, i, j)) |
| 114 | + s = heappop(pq)[3:] |
| 115 | + |
| 116 | + # 이동할 방향 결정 |
| 117 | + d = [0, 0] |
| 118 | + if s[0] != r: |
| 119 | + d[0] = (s[0] - r) // abs(s[0] - r) |
| 120 | + if s[1] != c: |
| 121 | + d[1] = (s[1] - c) // abs(s[1] - c) |
| 122 | + |
| 123 | + # 루돌프 이동 위치 |
| 124 | + nr = r + d[0] |
| 125 | + nc = c + d[1] |
| 126 | + |
| 127 | + return nr, nc, d |
| 128 | + |
| 129 | + |
| 130 | +def moveS(santa, rudolf): |
| 131 | + sd = ((-1, 0), (0, 1), (1, 0), (0, -1), (0, 0)) |
| 132 | + sr, sc = santa |
| 133 | + rr, rc = rudolf |
| 134 | + |
| 135 | + pq = [] |
| 136 | + heappush(pq, ((rr - sr) ** 2 + (rc - sc) ** 2, 4)) |
| 137 | + # 루돌프와의 최단거리 방향 찾기 |
| 138 | + for i in range(4): |
| 139 | + dr, dc = sd[i] |
| 140 | + nr = sr + dr |
| 141 | + nc = sc + dc |
| 142 | + if 0 <= nr < N and 0 <= nc < N and not board[nr][nc] > 0: |
| 143 | + heappush(pq, ((rr - nr) ** 2 + (rc - nc) ** 2, i)) |
| 144 | + dir = sd[heappop(pq)[1]] |
| 145 | + nr = sr + dir[0] |
| 146 | + nc = sc + dir[1] |
| 147 | + return nr, nc, dir |
| 148 | + |
| 149 | + |
| 150 | +def crashBySanta(r, c, dir): |
| 151 | + nr = r - dir[0] * D |
| 152 | + nc = c - dir[1] * D |
| 153 | + return nr, nc |
| 154 | + |
| 155 | + |
| 156 | +def crashByRudolf(r, c, dir): |
| 157 | + nr = r + dir[0] * C |
| 158 | + nc = c + dir[1] * C |
| 159 | + return nr, nc |
| 160 | + |
| 161 | + |
| 162 | +def crashSantas(r, c, num, dir): |
| 163 | + nr = r + dir[0] |
| 164 | + nc = c + dir[1] |
| 165 | + global out_count |
| 166 | + |
| 167 | + if not (0 <= nr < N and 0 <= nc < N): # out |
| 168 | + santa_state[num - 1] = -1 |
| 169 | + out_count += 1 |
| 170 | + return |
| 171 | + |
| 172 | + n_num = board[nr][nc] |
| 173 | + setSantaLocation(nr, nc, num) |
| 174 | + |
| 175 | + if n_num > 0: |
| 176 | + crashSantas(nr, nc, n_num, dir) |
| 177 | + |
| 178 | + |
| 179 | +print(" ".join(list(map(str, solution())))) |
0 commit comments