243. 마법사 상어와 비바라기

아현·2021년 8월 11일
0

Algorithm

목록 보기
254/400

백준




1. Python


import sys
input = sys.stdin.readline

n, m = map(int, input().split())
rain = [list(map(int, input().split())) for _ in range(n)]
cloud = [(n-1, 0), (n-1, 1), (n-2, 0), (n-2, 1)]
mlist = []

dx = [0, -1, -1, -1, 0, 1, 1, 1]
dy = [-1, -1, 0, 1, 1, 1, 0, -1]

for _ in range(m):
    a, b = map(int, input().split())
    mlist.append((a - 1, b))


for i in range(m):
    d, s = mlist[i]
    mcloud = []
    for i in range(len(cloud)):
        x, y = cloud[i]
        nx = (x + dx[d] * s) % n
        ny = (y + dy[d] * s) % n
        mcloud.append((nx, ny))

    for x, y in mcloud:
        rain[x][y] += 1 
        
    cloud.clear()

    for x, y in mcloud:
        count = 0
        for d in range(1, 8, 2):
            nx = x + dx[d]
            ny = y + dy[d]
            if 0 <= nx < n and 0 <= ny < n:
                if rain[nx][ny] > 0:
                    count += 1
        rain[x][y] += count

    for i in range(n):
        for j in range(n):
            if rain[i][j] >= 2 and (i, j) not in mcloud:
                cloud.append((i, j))
                rain[i][j] -= 2


answer = 0

for i in range(n):
    for j in range(n):
        if rain[i][j] > 0:
            answer += rain[i][j]

print(answer)






profile
For the sake of someone who studies computer science

0개의 댓글