백준 16235 나무재테크

문제

https://www.acmicpc.net/problem/16235

코드

n, m, k = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(n)]
ages = [[[] for _ in range(n)] for _ in range(n)]
for _ in range(m):
    x, y, z = map(int, input().split())
    ages[x-1][y-1].append(z)

time = 0
board = [[5]*n for _ in range(n)]
for i in range(n):
    for j in range(n):
        ages[i][j].sort()
d = [[0, 1], [0, -1], [1, 0], [-1, 0], [1, 1], [1, -1], [-1, 1], [-1, -1]]
while time < k:
    # 봄여름
    for i in range(n):
        for j in range(n):
            if len(ages[i][j]) == 0:
                continue
            idx = 0
            while idx < len(ages[i][j]) and ages[i][j][idx] <= board[i][j]:
                board[i][j] -= ages[i][j][idx]
                ages[i][j][idx] += 1
                idx += 1

            while idx < len(ages[i][j]):
                board[i][j] += ages[i][j][idx]//2
                del ages[i][j][idx]

    # 가을
    for i in range(n):
        for j in range(n):
            for age in ages[i][j]:
                if age % 5 == 0:
                    for dy, dx in d:
                        ny = i+dy
                        nx = j+dx
                        if 0 <= ny < n and 0 <= nx < n:
                            ages[ny][nx].insert(0, 1)
    # 겨울
    for i in range(n):
        for j in range(n):
            board[i][j] += arr[i][j]
    time += 1
answer = 0
for i in range(n):
    for j in range(n):
        answer += len(ages[i][j])
print(answer)

풀이

시뮬레이션 문제입니다 문제 요구사항대로 봄 여름 가을 겨울을 나눠서 각각 구현해줬습니다

좋은 웹페이지 즐겨찾기