BOJ 1149 RGB거리

1 개요[ | ]

BOJ 1149 RGB거리


2 같이 보기[ | ]

3 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int N;
int cost[3];
int total[1001][3] = {};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    for(int i=1; i<=N; i++) {
        cin >> cost[0] >> cost[1] >> cost[2];
        total[i][0] = min(total[i-1][1], total[i-1][2]) + cost[0];
        total[i][1] = min(total[i-1][2], total[i-1][0]) + cost[1];
        total[i][2] = min(total[i-1][0], total[i-1][1]) + cost[2];
    }
    cout << min(total[N][0], min(total[N][1], total[N][2]));
}
#include <bits/stdc++.h>
using namespace std;

int N;
int cost[3], total[3], Prev[3] = {};

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    for(int i=0; i<N; i++) {
        cin >> cost[0] >> cost[1] >> cost[2];
        total[0] = min(Prev[1], Prev[2]) + cost[0];
        total[1] = min(Prev[2], Prev[0]) + cost[1];
        total[2] = min(Prev[0], Prev[1]) + cost[2];
        Prev[0]=total[0], Prev[1]=total[1], Prev[2]=total[2];
    }
    cout << min(min(total[0], total[1]), total[2]);
}
#include <bits/stdc++.h>
using namespace std;

int N;
int cost1, cost2, cost3;
int total1, total2, total3;
int prev1=0, prev2=0, prev3=0;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    for(int i=1; i<=N; i++) {
        cin >> cost1 >> cost2 >> cost3;
        total1 = min(prev2, prev3) + cost1;
        total2 = min(prev3, prev1) + cost2;
        total3 = min(prev1, prev2) + cost3;
        prev1 = total1, prev2 = total2, prev3 = total3;
    }
    cout << min(min(total1, total2), total3);
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}