개발자 톡

연습문제 톡 나무 수확

DP로 풀이하였는데 테케 12번에 대하여 반례가 궁금합니다

등록일
2024-06-28 11:26:42
조회수
38
작성자
qhdrmfdl123
import sys


n = int(input())
field = [list(map(int, input().split())) for _ in range(n)]
dp = [[[0, 0] for _ in range(n)] for _ in range(n)]
dp[0][0] = [field[0][0]*2, field[0][0]]


for i in range(n):
    for j in range(n):
        candits = []
        for dx, dy in [(-1, 0), (0, -1)]:
            bx, by = i+dx, j+dy
            if bx < 0 or bx >= n or by < 0 or by >= n:
                continue


            if dp[bx][by][1] < field[i][j]:
                tsum_value, tmax_value = dp[bx][by][0] - dp[bx][by][1] + 2*field[i][j], field[i][j]
                if dp[i][j][0] <= tsum_value:
                    candits.append((tsum_value, tmax_value))
            else:
                tsum_value = dp[bx][by][0] + field[i][j]
                if dp[i][j][0] <= tsum_value:
                    candits.append((tsum_value, dp[bx][by][1]))
        if not candits:
            continue
        candits.sort(key=lambda x: (-x[0], x[1]))
        dp[i][j][0], dp[i][j][1] = candits[0][0], candits[0][1]
    
print(dp[-1][-1][0])  
#나무_수확

이 카테고리의 톡 더보기