PS/This
정수 삼각형
RUBY_루비
2020. 8. 15. 00:00
출처:: https://www.acmicpc.net/problem/1932
분류:: 다이나믹프로그래밍 DP
1. 문제 이해 및 해결과정
- dp[i][j] = arr[i][j] + max(dp[i-1][j-1],dp[i-1][j]) - dp배열에 저장되는 값은 현재 위치값 + max( 입력조건 기준으로는 왼쪽위 대각선 값 or 위쪽 값) - 단!!! 처리해야 할 사항은 맨 윗줄 혹은 사이드일 때 왼쪽 or 오른쪽 대각선 값이 없으므로 처리 해야함 => 맨 왼쪽 j==0일때는 왼쪽 대각선이 없음 , 맨 오른쪽 j==len(dp[i])-1 일때는 오른쪽 대각선이 없음 |
2. 풀이방법
1. dp
#정수 삼각형
#https://www.acmicpc.net/problem/1932
import sys
sys.stdin = open("input.txt","r")
n=int(input())
dp=[list(map(int,input().split())) for _ in range(n)] #dp테이블
for i in range(1,n):
for j in range(len(dp[i])):
if j==0: #왼쪽 위 대각선이 없음
dp[i][j]+=dp[i-1][j]
elif j==len(dp[i])-1:#오른쪽 대각선이 없음
dp[i][j]+=dp[i-1][j-1]
else:
dp[i][j]+=max(dp[i-1][j],dp[i-1][j-1])
print(max(dp[n-1]))
3. 오답원인
4. 알게된 점