백준/다이나믹 프로그래밍
# 1149 RGB 거리
bright_code
2020. 9. 11. 13:06
728x90
반응형
# 틀린 코드
n = int(input()) # 2<= n <= 1000
cost = [0]*(n+1)
for i in range(1,n+1):
cost[i] = list(map(int,input().split()))
# 집 마다 칠하는 비용이 다름!
id = [0 for i in range(n+1)]
id[0] = cost[1].index(max(cost[1]))
total=0
for i in range(1,n+1):
id[i] = cost[i].index(min(cost[i]))
if (id[i]==id[i-1]) :
id[i] = cost[i].index( min ( cost[i][ (id[i]+1)%3 ], cost[i][ (id[i]+2)%3 ]) )
total += cost[i][ id[i] ]
print(total)
# 정답
n = int(input())
cost = []
for i in range(n):
cost.append(list(map(int, input().split())))
for i in range(1, len(p)):
cost[i][0] = min(cost[i - 1][1], cost[i - 1][2]) + cost[i][0]
cost[i][1] = min(cost[i - 1][0], cost[i - 1][2]) + cost[i][1]
cost[i][2] = min(cost[i - 1][0], cost[i - 1][1]) + cost[i][2]
print(min(cost[n - 1][0], cost[n - 1][1], cost[n - 1][2]))
728x90
반응형