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)