https://www.acmicpc.net/problem/20056
def move():
global board
new_board = [[[] for _ in range(n+1)] for _ in range(n+1)]
for y in range(1, n+1):
for x in range(1, n+1):
for m, s, d in board[y][x]:
ny, nx = get_next(y, x, s, d)
new_board[ny][nx].append([m, s, d])
board = new_board
def get_next(y, x, s, d):
for _ in range(s):
dy, dx = dirs[d]
y = (y + dy - 1) % n + 1
x = (x + dx - 1) % n + 1
return y, x
def change():
for y in range(1, n+1):
for x in range(1, n+1):
if len(board[y][x]) < 2:
continue
new_fires = []
sum_m, sum_s = 0, 0
all_odd, all_even = True, True
for m, s, d in board[y][x]:
sum_m += m
sum_s += s
if d % 2 == 1:
all_even = False
else:
all_odd = False
tmp = 1
if all_even or all_odd:
tmp = 0
for i in range(4):
if sum_m // 5:
new_fires.append([sum_m//5, sum_s//len(board[y][x]), 2*i+tmp])
board[y][x] = new_fires
def get_sum():
summ = 0
for y in range(1, n+1):
for x in range(1, n+1):
for m, s, d in board[y][x]:
summ += m
return summ
# init
import sys
read = sys.stdin.readline
n, m, k = map(int, read().split())
board = [[[] for _ in range(n+1)] for _ in range(n+1)]
for _ in range(m):
r,c,m,s,d = map(int, read().split())
board[r][c].append([m,s,d])
dirs = [[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]]
# start
for _ in range(k):
move()
change()
print(get_sum())