|
| 1 | +# https://www.acmicpc.net/problem/17404 |
| 2 | +# RGB거리2, 골드4 |
| 3 | + |
| 4 | +import sys |
| 5 | +import copy |
| 6 | + |
| 7 | +N = int(input()) |
| 8 | +costs = [] |
| 9 | +for _ in range(N): |
| 10 | + costs.append(list(map(int,input().split(" ")))) |
| 11 | + |
| 12 | +INF = float('inf') |
| 13 | +answer = INF |
| 14 | + |
| 15 | +for first in range(3): # 첫 집 색 고정: 0(R),1(G),2(B) |
| 16 | + dp = [[INF] * 3 for _ in range(N)] |
| 17 | + |
| 18 | + # index == 0 초기화: first만 허용, 나머지는 INF로 막기 |
| 19 | + dp[0][first] = costs[0][first] |
| 20 | + |
| 21 | + for index in range(1, N): |
| 22 | + # 일반 RGB거리 점화식 |
| 23 | + dp[index][0] = min(dp[index-1][1], dp[index-1][2]) + costs[index][0] |
| 24 | + dp[index][1] = min(dp[index-1][0], dp[index-1][2]) + costs[index][1] |
| 25 | + dp[index][2] = min(dp[index-1][0], dp[index-1][1]) + costs[index][2] |
| 26 | + |
| 27 | + # 마지막 집은 첫 집(first)과 다른 색만 가능 |
| 28 | + for last in range(3): |
| 29 | + if last != first: |
| 30 | + answer = min(answer, dp[N-1][last]) |
| 31 | + |
| 32 | +print(answer) |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +# 시도1 : 완탐 |
| 39 | +# color_set = set([0,1,2]) |
| 40 | + |
| 41 | +# INF = float('inf') |
| 42 | +# answer = INF |
| 43 | +# # prev : 이전색, sum : 누적, index : 현재위치, start : 시작색 |
| 44 | +# def recursive(prev,sum,index,start,test_sum): |
| 45 | +# global answer |
| 46 | + |
| 47 | +# # 이미 sum을 넘은 경우에는 미리 종료 |
| 48 | +# if sum > INF: |
| 49 | +# return |
| 50 | + |
| 51 | +# print(f"이전 : {prev}, 누적 : {sum}, 현재 위치 : {index}, 시작 : {start}") |
| 52 | +# print(f"현재 배열 : {test_sum}\n") |
| 53 | +# # 종료 조건 |
| 54 | +# if index >= N: |
| 55 | +# answer = min(answer,sum) |
| 56 | +# return |
| 57 | + |
| 58 | +# can_colors = set(color_set) |
| 59 | +# can_colors.remove(prev) |
| 60 | + |
| 61 | +# # 마지막인 경우 |
| 62 | +# if index == N-1 and start in can_colors: |
| 63 | +# print(f"마지막 이며 : 시작 : {start}, can_colors : {can_colors}, 동시 존재 : {start in can_colors}") |
| 64 | +# can_colors.remove(start) |
| 65 | + |
| 66 | +# for color in can_colors: |
| 67 | +# new_test_sum = copy.deepcopy(test_sum) |
| 68 | +# new_test_sum.append(color) |
| 69 | +# recursive(color,sum+costs[index][color],index+1,start,new_test_sum) |
| 70 | + |
| 71 | +# for color in color_set: |
| 72 | +# recursive(color,costs[0][color],1,color,[color]) |
| 73 | + |
| 74 | +# print(answer) |
0 commit comments