๐๏ธ ๊ฒช์ ๋ฌธ์
- DFS ๋ฐฑํธ๋ํน์ผ๋ก ํ์๋ค๊ฐ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฐ์ํ๋ค.
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ
- ์๊ณ ๋ฆฌ์ฆ ๋ณด๊ธฐ๋ฅผ ๋๋ฌ๋ณด๊ณ DP๋ก ํ์ด์ผ ํ๋ค๋ ์ ์ ์๊ฒ๋๋ค. ๊ทธ๋์ DP๋ก ํ์ดํ๋๋ฐ ๋๋ฌด ์ค๋๋ง์ DP๋ฌธ์ ํผ์ ์ฑ๊ณตํด์ ๋ฟ๋ฏํด์ ๋ก๊ทธ๋ฅผ ๋จ๊ธด๋ค ๐ฅน
- ๊ทผ๋ฐ, DP๋ก ํ์ด์ผํ๋ ๋ฌธ์ ์ธ์ง๋ฅผ ์ด๋ป๊ฒ ์ ์ ์์๊น?
- ๊ฐ ์ง๋ง๋ค 3๊ฐ์ง ์์์ ์ ํํ ์ ์์. ์ต์
์ ๊ฒฝ์ฐ์ 3^1000์ด ๋จ.
- int์ ๋ฒ์๊ฐ -2^31 ~ 2^31 - 1 ์ด๋ฏ๋ก, ์~~~~์ฒญ ํฐ ์๋ค.
๐ ์ฝ๋
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
struct Price {
int r;
int g;
int b;
};
enum COLOR {
RED,
GREEN,
BLUE,
COLOR_MAX
};
int N;
vector<Price> prices;
int dp[1001][COLOR_MAX];
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N;
for (int i=0; i<N; i++) {
int r, g, b;
cin >> r >> g >> b;
prices.push_back({r, g, b});
}
dp[0][RED] = prices[0].r;
dp[0][GREEN] = prices[0].g;
dp[0][BLUE] = prices[0].b;
for (int i=1; i<N; i++) {
dp[i][RED] = min(dp[i-1][GREEN] + prices[i].r,
dp[i-1][BLUE] + prices[i].r);
dp[i][GREEN] = min(dp[i-1][RED] + prices[i].g,
dp[i-1][BLUE] + prices[i].g);
dp[i][BLUE] = min(dp[i-1][RED] + prices[i].b,
dp[i-1][GREEN] + prices[i].b);
}
int minPrice = min({dp[N-1][RED], dp[N-1][GREEN], dp[N-1][BLUE]});
cout << minPrice << '\n';
return 0;
}