DevYoon
[๋ฐฑ์ค] 20056. ๋ง๋ฒ์ฌ ์์ด์ ํ์ด์ด๋ณผ (Python) ๋ณธ๋ฌธ
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)