|
| 1 | +''' |
| 2 | +BOJ #20056. 마법사 상어와 파이어볼 (골드 4) |
| 3 | +https://www.acmicpc.net/problem/20056 |
| 4 | +유형: Implementation, Simulation |
| 5 | +''' |
| 6 | + |
| 7 | +N, M, K = map(int, input().split()) |
| 8 | +fireballs = [] |
| 9 | +for _ in range(M): |
| 10 | + _r, _c, _m, _s, _d = list(map(int, input().split())) |
| 11 | + fireballs.append([_r-1, _c-1, _m, _s, _d]) |
| 12 | + |
| 13 | +MAP = [[[] for _ in range(N)] for _ in range(N)] |
| 14 | + |
| 15 | +dx = [-1, -1, 0, 1, 1, 1, 0, -1] |
| 16 | +dy = [0, 1, 1, 1, 0, -1, -1, -1] |
| 17 | + |
| 18 | +for _ in range(K): |
| 19 | + # 파이어볼 이동 |
| 20 | + while fireballs: |
| 21 | + cr, cc, cm, cs, cd = fireballs.pop(0) |
| 22 | + nr = (cr + cs * dx[cd]) % N # 1번-N번 행 연결되어있기 때문 |
| 23 | + nc = (cc + cs * dy[cd]) % N |
| 24 | + MAP[nr][nc].append([cm, cs, cd]) |
| 25 | + |
| 26 | + # 2개 이상인지 체크 |
| 27 | + for r in range(N): |
| 28 | + for c in range(N): |
| 29 | + # 2개 이상인 경우 -> 4개의 파이어볼로 쪼개기 |
| 30 | + if len(MAP[r][c]) > 1: |
| 31 | + sum_m, sum_s, cnt_odd, cnt_even, cnt = 0, 0, 0, 0, len(MAP[r][c]) |
| 32 | + while MAP[r][c]: |
| 33 | + _m, _s, _d = MAP[r][c].pop(0) |
| 34 | + sum_m += _m |
| 35 | + sum_s += _s |
| 36 | + if _d % 2: |
| 37 | + cnt_odd += 1 |
| 38 | + else: |
| 39 | + cnt_even += 1 |
| 40 | + if cnt_odd == cnt or cnt_even == cnt: # 모두 홀수이거나 모두 짝수인 경우 |
| 41 | + nd = [0, 2, 4, 6] |
| 42 | + else: |
| 43 | + nd = [1, 3, 5, 7] |
| 44 | + if sum_m//5: # 질량 0이면 소멸 |
| 45 | + for d in nd: |
| 46 | + fireballs.append([r, c, sum_m//5, sum_s//cnt, d]) |
| 47 | + |
| 48 | + # 1개인 경우 |
| 49 | + if len(MAP[r][c]) == 1: |
| 50 | + fireballs.append([r, c] + MAP[r][c].pop()) |
| 51 | + |
| 52 | +print(sum([f[2] for f in fireballs])) |
0 commit comments