DevYoon

[λ°±μ€€] 20056. λ§ˆλ²•μ‚¬ 상어와 νŒŒμ΄μ–΄λ³Ό (Python) λ³Έλ¬Έ

PS/Baekjoon

[λ°±μ€€] 20056. λ§ˆλ²•μ‚¬ 상어와 νŒŒμ΄μ–΄λ³Ό (Python)

gimewn 2022. 9. 6. 20:29

link πŸ”— https://www.acmicpc.net/problem/20056

 

20056번: λ§ˆλ²•μ‚¬ 상어와 νŒŒμ΄μ–΄λ³Ό

첫째 쀄에 N, M, Kκ°€ 주어진닀. λ‘˜μ§Έ 쀄뢀터 M개의 쀄에 νŒŒμ΄μ–΄λ³Όμ˜ 정보가 ν•œ 쀄에 ν•˜λ‚˜μ”© 주어진닀. νŒŒμ΄μ–΄λ³Όμ˜ μ •λ³΄λŠ” λ‹€μ„― μ •μˆ˜ ri, ci, mi, si, di둜 이루어져 μžˆλ‹€. μ„œλ‘œ λ‹€λ₯Έ 두 νŒŒμ΄μ–΄λ³Όμ˜ μœ„μΉ˜

www.acmicpc.net

 

🦈 νŒŒμ΄μ–΄λ³Όμ„ λͺ¨λ‘ 합쳐 λ‚˜λˆŒ λ•Œ, 0이면 μ†Œλ©Έλœλ‹€λŠ” 쑰건을 μ μš©ν•˜μ§€ μ•Šμ•„μ„œ 닡이 ν•œλ™μ•ˆ μ•ˆ λ‚˜μ™”μ—ˆλ‹€.

🦈 ν‰μ†Œμ™€ 달리 2차원 board 리슀트λ₯Ό 생성할 λ•Œ 0이 μ•„λ‹Œ []으둜 μƒμ„±ν•΄μ€¬λŠ”λ°, 이λ₯Ό κ³ λ €ν•˜μ§€ μ•Šμ•„μ„œ 인덱슀 μ—λŸ¬κ°€ λ‚˜μ™”μ—ˆλ‹€.

 

import sys
input = sys.stdin.readline

dir = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def cal(y, x):
    length = len(board[y][x])
    tempM = 0
    tempS = 0
    tempOdd = 0 # λ°©ν–₯이 ν™€μˆ˜μΌ λ•Œ
    tempEven = 0 # λ°©ν–₯이 짝수일 λ•Œ
    for m, s, d in board[y][x]:
        tempM += m # μ§ˆλŸ‰ λ”ν•˜κΈ°
        tempS += s # 속λ ₯ λ”ν•˜κΈ°
        if d % 2: # λ°©ν–₯이 μ§μˆ˜μΈμ§€ ν™€μˆ˜μΈμ§€ ꡬ뢄
            tempOdd += 1
        else:
            tempEven += 1

    tempM //= 5
    tempS //= length

    if tempOdd == length or tempEven == length: # λ°©ν–₯이 λͺ¨λ‘ μ§μˆ˜μ΄κ±°λ‚˜ λͺ¨λ‘ ν™€μˆ˜μ΄κ±°λ‚˜
        changeDir = [0, 2, 4, 6]
    else:
        changeDir = [1, 3, 5, 7]

    if tempM: # μ§ˆλŸ‰μ΄ 0인 νŒŒμ΄μ–΄λ³Όμ€ μ†Œλ©Έμ΄λ―€λ‘œ
        for cd in changeDir: # λ°©ν–₯ 순으둜
            fireball.append([y, x, tempM, tempS, cd]) # νŒŒμ΄μ–΄λ³Ό μΆ”κ°€
    board[y][x] = [] # μ›λž˜ μΉΈ μ΄ˆκΈ°ν™”

def moveFireball(fb):
    y, x, m, s, d = fb
    my = (y+dir[d][0]*s)%N # y 이동, 처음과 끝 μ—°κ²°
    mx = (x + dir[d][1] * s) % N # x 이동, 처음과 끝 μ—°κ²°
    board[my][mx].append([m, s, d]) # 칸에 μ‚½μž…

N, M, K = map(int, input().split())
board = [[[] for _ in range(N)] for _ in range(N)]
fireball = []
res = 0

for _ in range(M):
    y, x, m, s, d = map(int, input().split()) # y, x, μ§ˆλŸ‰, 속λ ₯, λ°©ν–₯
    fireball.append([y-1, x-1, m, s, d])

for _ in range(K):
    # νŒŒμ΄μ–΄λ³Ό 이동
    while fireball:
        fb = fireball.pop(0)
        moveFireball(fb)
    # 이동이 λλ‚œ ν›„
    for y in range(N):
        for x in range(N):
            if len(board[y][x]) >= 2:
                cal(y, x)
            elif len(board[y][x]) == 1:
                m, s, d = board[y][x][0]
                fireball.append([y, x, m, s, d])
                board[y][x] = []

for fb in fireball:
    res += fb[2]

print(res)