https://www.acmicpc.net/problem/16235
def spring_summer():
for i in range(n):
for j in range(n):
new_tree = deque()
while tree[i][j]:
curr = tree[i][j].popleft()
if food[i][j] >= curr:
food[i][j] -= curr
new_tree.append(curr + 1)
else:
tree[i][j].appendleft(curr)
break
while tree[i][j]:
curr = tree[i][j].popleft()
food[i][j] += curr // 2
tree[i][j] = new_tree
def fall():
for i in range(n):
for j in range(n):
for y in tree[i][j]:
if y % 5 == 0:
append_tree(i, j)
def append_tree(cy, cx):
dy = [0,0,-1,1,-1,1,-1,1]
dx = [1,-1,0,0,-1,1,1,-1]
for d in range(8):
ny, nx = cy + dy[d], cx + dx[d]
if 0 <= ny < n and 0 <= nx < n:
tree[ny][nx].appendleft(1)
def winter():
for i in range(n):
for j in range(n):
food[i][j] += added_food[i][j]
def get_answer():
answer = 0
for i in range(n):
for j in range(n):
answer += len(tree[i][j])
return answer
# init
from collections import deque
import sys, heapq
read = sys.stdin.readline
n, m, k = map(int, read().split())
added_food = [list(map(int, read().split())) for _ in range(n)]
food = [[5]*n for _ in range(n)]
tree = [[deque() for _ in range(n)] for _ in range(n)]
inputs = [list(map(int, read().split())) for _ in range(m)]
for y, x, year in inputs:
tree[y-1][x-1].append(year)
# start
for y in range(k):
spring_summer()
fall()
winter()
print(get_answer())