https://www.acmicpc.net/problem/1932
1932번: 정수 삼각형
문제 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 위 그림은 크기가 5인 정수 삼각형의 한 모습이다. 맨 위층 7부터 시작해서 아래에 있는 수 중 하나를 선택하여 아래층으로 내려올 때, 이제까지 선택된 수의 합이 최�
www.acmicpc.net
# 정수 삼각형
N = int(input())
dp = []
for input_line in range(N):
dp.append(list(map(int, input().split(' '))))
for current_line in range(1, N):
for current_col_index in range(len(dp[current_line])):
# 현재 수가 현재 라인의 맨 왼쪽 수일 때,
if current_col_index == 0:
# 위 라인의 맨 왼쪽 값이 됨.
dp[current_line][current_col_index] += dp[current_line - 1][current_col_index]
continue
# 현재 수가 현재 라인의 맨 오른쪽의 수일 때,
if current_col_index == len(dp[current_line]) - 1:
# 위 라인의 맨 오른쪽 값이 됨.
dp[current_line][current_col_index] += dp[current_line - 1][current_col_index - 1]
continue
# 위의 두 경우가 아니라면,
# 왼쪽 위 대각선, 오른쪽 위 대각선 중에서 큰 값을 더해야 함.
dp[current_line][current_col_index] += max(dp[current_line - 1][current_col_index - 1],
dp[current_line - 1][current_col_index])
print(max(dp[N - 1]))
'Problem-solving > 백준' 카테고리의 다른 글
백준 - 문자열(1120번) (Python3) (0) | 2020.05.23 |
---|---|
백준 - 거스름돈(5585번) (Python3) (0) | 2020.05.23 |
백준 - 플로이드(11404번) (Python3) (0) | 2020.05.21 |
백준 - 그룹 단어 체커(1316번) (Python3) (0) | 2020.05.21 |
백준 - ATM(11399번) (Python3) (0) | 2020.05.21 |
댓글