๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ๐Ÿฃ: ๐’œ๐“๐‘”๐‘œ๐“‡๐’พ๐“‰๐’ฝ๐“‚/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด

BOJ17404 : RGB๊ฑฐ๋ฆฌ2 (Gold 4)

#include <iostream>
using namespace std;
int cost[1000][3];
long long costDP[1000][3];
long long result[3];
//long long result = 1000 * 10000 + 1;

void makeDP(int N)
{
    for (int j = 0; j < 3; j++)
    {
        costDP[0][0] = 1000 * 10000 + 1;
        costDP[0][1] = 1000 * 10000 + 1;
        costDP[0][2] = 1000 * 10000 + 1;
        costDP[0][j] = cost[0][j];
        for (int i = 1; i < N; i++)
        {
            costDP[i][0] = min(costDP[i - 1][1], costDP[i - 1][2]) + cost[i][0];
            costDP[i][1] = min(costDP[i - 1][0], costDP[i - 1][2]) + cost[i][1];
            costDP[i][2] = min(costDP[i - 1][0], costDP[i - 1][1]) + cost[i][2];
        }
        costDP[N - 1][j] = 1000 * 10000 + 1;
        result[j] = min(costDP[N - 1][0], costDP[N - 1][1]) > costDP[N - 1][2] ? costDP[N - 1][2] : min(costDP[N - 1][0], costDP[N - 1][1]);
        /*  for (int i = 0; i < 3; i++)
        {
            if (j == i)
                continue;
            result = min(result, costDP[N - 1][i]);
        }
        */
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int N;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> cost[i][0] >> cost[i][1] >> cost[i][2];
    }
    makeDP(N);
    long long resultCost = min(result[0], result[1]) > result[2] ? result[2] : min(result[0], result[1]);
    cout << resultCost;
}

์—ญ์‹œ ์ตœ์†Œ์ •์€ ๋ฐ”๋ณด๊ฐ€ ๋งž์•˜ใ„ท

RGB๊ฑฐ๋ฆฌ1์—์„œ ๋งž์€ ๋ถ€๋ถ„ ์‹ ๊ฒฝ๋„ ์•ˆ์“ฐ๊ณ  ๋Œ€์ฒด ์™œ ํ‹€๋ฆฐ๊ฑด์ง€ ํ•œ์ฐธ์„ ์ณ๋‹ค๋ดค๋Š”๋ฐ costDP ๋ฐฐ์—ด ํ• ๋‹น์„ 2๋กœ ์คฌ์—Œใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹ใ…‹์•„๋‹ˆ๊ทผ๋ฐ ์™œ ๋งž์€๊ฑด๋ฐ 1์—์„œ๋Š” ์ง„์งœ ์–ด์ด๊ฐ€ ์—†๋„ค.....