[백준]1149번: RGB거리

yewon Lee·2023년 7월 19일
0

😎BACKJOON>1149번: RGB거리


📘 문제풀이

i일 때 red, green, blue 비용과 i-1일 때 i색과 겹치지 않는 비용 중
더 작은 값을 i의 rgb 비용으로 한다.

import sys

n = int(sys.stdin.readline())
red = [0 for _ in range(n+1)]
green = [0 for _ in range(n+1)]
blue = [0 for _ in range(n+1)]
cost = [[0,0,0] for _ in range(n+1)]

for i in range(1, n+1):
    red[i], green[i], blue[i] = map(int, sys.stdin.readline().split())

cost[1][0] = red[1]
cost[1][1] = green[1]
cost[1][2] = blue[1]

for i in range(2, n+1):
    cost[i][0] = red[i]
    cost[i][1] = green[i]
    cost[i][2] = blue[i]
    
    cost[i][0] = min(cost[i][0]+cost[i-1][1], cost[i][0]+cost[i-1][2])
    cost[i][1] = min(cost[i][1]+cost[i-1][0], cost[i][1]+cost[i-1][2])
    cost[i][2] = min(cost[i][2]+cost[i-1][0], cost[i][2]+cost[i-1][1])

print(min(cost[n]))
profile
시작

1개의 댓글

comment-user-thumbnail
2023년 7월 19일

뛰어난 글이네요, 감사합니다.

답글 달기