π λ°±μ€ RGB거리 (c++)
μ²μμλ μ¬κ·ν¨μλ₯Ό μ¬μ©ν΄μ ν μ μμ κ²μ΄λΌκ³ μκ°ν΄μ μ¬κ·λ‘ λ¬Έμ λ₯Ό νμ΄λ³΄μλ€.
νμ§λ§ μκ°μ΄κ³Όκ° λ°μνμκ³ μ§λ¬Έμ 보λ€λ³΄λ DPλ₯Ό μ¬μ©ν΄μ νΈλ λ°©λ²μ΄ μλ€λ κ²μ μκ²λμλ€.
λ΄ λ‘μ§μ κ°λ¨ν μ€λͺ νλ©΄ λ€μκ³Ό κ°λ€.
- 1λ²μ§Έ(μμ)μ ν΄λΉνλ κ° μ(R,G,B)μ κ°κ²©μ minCostsλΌλ λ°°μ΄μ λ£λλ€.
- i = 2 .. N κΉμ§ κ° μ(R,G,B)μ μ ννμ λ ꡬν μ μλ μ΅μ κ°κ²©μ minCosts[i]μ λ£λλ€.
--> RμΌ κ²½μ° G,Bλ§ / GμΌ κ²½μ° R,Bλ§ / BμΌ κ²½μ° R,Gλ§ μ ν κ°λ₯
--> μ νκ°λ₯ν μ μ€ κ°κ²©μ΄ μμ μμ μ ννλ©΄ κ·Έκ²μ΄ μ΅μ μ μ νμ΄ λλ€.- minCosts[N]μ κ°μ΄ ꡬν μ μλ μ΅μκ°μ΄ λλ€.
#include <iostream>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
int N;
vector<vector<int>> costs; //κ°κ²©
vector<vector<int>> minCosts(1001, {INT_MAX,INT_MAX,INT_MAX}); //κ° μμΉ λ³ μ΅μκ°κ²©
//ν΄λΉ indexμμ κ° μμ μ ννμ λ λμ¬μ μλ μ΅μ κ°κ²©μ ꡬν¨
void getCosts(int index) {
for (int i = 0; i < 3; i++) {
vector<int> canCosts;
for (int k = 0; k < 3; k++) {
if (k != i) {
canCosts.push_back(minCosts[index-1][k]);
}
}
int selected = min(canCosts[0], canCosts[1]);
int nowCost = selected + costs[index][i];
minCosts[index][i] = min(minCosts[index][i], nowCost);
}
}
int main() {
cin >> N; //μ
λ ₯
costs.push_back({});
for (int i = 0; i < N; i++) {
int c1, c2, c3; cin >> c1 >> c2 >> c3;
costs.push_back({ c1,c2,c3 });
}
minCosts[1] = costs[1]; //μμ
for (int i = 2; i <= N; i++) {
getCosts(i);
}
int answer = min(minCosts[N][0], minCosts[N][1]);
answer = min(answer, minCosts[N][2]);
cout << answer << endl;
return 0;
}