20057번 - 마법사 상어와 토네이도
풀이
import sys
n = int(sys.stdin.readline())
desert = [list(map(int,sys.stdin.readline().split())) for _ in range(n)]
now = [n//2,n//2]
left = [(-2, 0, 0.02), (2, 0, 0.02), (-1, -1, 0.1), (-1, 0, 0.07), (-1, 1, 0.01), (1, -1, 0.1), (1, 0, 0.07), (1, 1, 0.01), (0, -2, 0.05), (0, -1, 0)]
right = [(x,-y,rate) for x, y,rate in left]
down = [(-y,x,rate) for x, y,rate in left]
up = [(-x,y,rate) for x, y,rate in down]
answer = 0
def move(cnt,dx,dy,direction):
global answer
for _ in range(cnt+1):
now[0], now[1] = now[0] + dx, now[1] + dy
if now[0] < 0 or now[1] < 0:
break
spreads = 0
for dx, dy, r in direction:
nx,ny = now[0] + dx, now[1] + dy
if r == 0:
sand = desert[now[0]][now[1]] - spreads
else:
sand = int(desert[now[0]][now[1]] * r)
if 0 <= nx < n and 0 <= ny < n:
desert[nx][ny] += sand
else:
answer += sand
spreads += sand
for i in range(n):
if i % 2 == 0:
move(i, 0, -1, left)
move(i, 1, 0, down)
else:
move(i, 0, 1, right)
move(i, -1, 0, up)
print(answer)