알고리즘/DP(다이나믹 프로그래밍)

[백준/DP/C++] 1149번 RGB거리 *

데메즈 2023. 2. 15. 10:32
728x90
반응형

문제는 여기!

#include <bits/stdc++.h>

using namespace std;
int N;
int cost[3];
int house[1001][3];

void input() {
    cin >> N;
    for(int i=0; i<N; i++){
        cin >> cost[0] >> cost[1] >> cost[2];
        if(i==0){
            house[0][0] = cost[0];
            house[0][1] = cost[1];
            house[0][2] = cost[2];
        } else {
            house[i][0] = min(house[i-1][1], house[i-1][2]) + cost[0];
            house[i][1] = min(house[i-1][0], house[i-1][2]) + cost[1];
            house[i][2] = min(house[i-1][0], house[i-1][1]) + cost[2];
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    input();
    cout << min(house[N-1][0], min(house[N-1][1], house[N-1][2]));

    return 0;
}
728x90
반응형