웹찢남

[백준 1149 RGB거리 문제] PYTHON 본문

백준 Algorithm

[백준 1149 RGB거리 문제] PYTHON

harry595 2021. 4. 14. 01:13

 

단순하게 보면 쉬운 문제였는데 너무 복잡하게 생각한 문제다.

빨 초 파가 전집과 겹치지 않게 하려면

cost[i][2]=min(cost[i-1][0],cost[i-1][1])+cost[i][2]

위의 방식으로 전 집의 색 cost 최솟 값을 더해 이차원 배열로 모든 집들에 cost를 부여하면 된다.

 

N = int(input())
cost= []
for _ in range(N):
    cost.append(list(map(int, input().split())))
for i in range(1,len(cost)):
    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]))

 

 

Comments