๐Ÿ™‡โ€โ™€๏ธ[Silver I] RGB๊ฑฐ๋ฆฌ - 1149

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2032 KB, ์‹œ๊ฐ„: 0 ms

๋ถ„๋ฅ˜

๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ

์ œ์ถœ ์ผ์ž

2024๋…„ 1์›” 7์ผ 19:49:50

๋ฌธ์ œ ์„ค๋ช…

RGB๊ฑฐ๋ฆฌ์—๋Š” ์ง‘์ด N๊ฐœ ์žˆ๋‹ค. ๊ฑฐ๋ฆฌ๋Š” ์„ ๋ถ„์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ N๋ฒˆ ์ง‘์ด ์ˆœ์„œ๋Œ€๋กœ ์žˆ๋‹ค.

์ง‘์€ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘ ์ค‘ ํ•˜๋‚˜์˜ ์ƒ‰์œผ๋กœ ์น ํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ๊ฐ์˜ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ๊ทœ์น™์„ ๋งŒ์กฑํ•˜๋ฉด์„œ ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•ด๋ณด์ž.

  • 1๋ฒˆ ์ง‘์˜ ์ƒ‰์€ 2๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • N๋ฒˆ ์ง‘์˜ ์ƒ‰์€ N-1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  • i(2 โ‰ค i โ‰ค N-1)๋ฒˆ ์ง‘์˜ ์ƒ‰์€ i-1๋ฒˆ, i+1๋ฒˆ ์ง‘์˜ ์ƒ‰๊ณผ ๊ฐ™์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ์ง‘์˜ ์ˆ˜ N(2 โ‰ค N โ‰ค 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ง‘์„ ๋นจ๊ฐ•, ์ดˆ๋ก, ํŒŒ๋ž‘์œผ๋กœ ์น ํ•˜๋Š” ๋น„์šฉ์ด 1๋ฒˆ ์ง‘๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์€ 1,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ชจ๋“  ์ง‘์„ ์น ํ•˜๋Š” ๋น„์šฉ์˜ ์ตœ์†Ÿ๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

์ฒ˜์Œ์— ์™„์ „ํƒ์ƒ‰์œผ๋กœ DFS๋ฅผ ์ด์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๊ณ ํ–ˆ๋‹ค.

void DFS(int L, int cost, char ch)
{
	if (L == n - 1)
	{
		res = min(res, cost);
		return;
	}
	else
	{
		if (ch != 'R')
			DFS(L + 1, cost + houses[L + 1].r, 'R');
		if (ch != 'G')
			DFS(L + 1, cost + houses[L + 1].g, 'G');
		if (ch != 'B')
			DFS(L + 1, cost + houses[L + 1].b, 'B');
	}
}

์ด๋ ‡๊ฒŒ ๋กœ์ง์„ ์งœ๋‹ˆ๊นŒ ์‹œ๊ฐ„์ œํ•œ์ด ๋‚˜์™”๋‹ค.

์˜ค๋‹ต ๋ฒ„์ „์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

struct House
{
	int r, g, b;
};

int n, res = 123456789;
vector<House> houses;
void setting()
{
	cin >> n;
	int r, g, b;
	for (int i = 0; i < n; ++i)
	{
		cin >> r >> g >> b;
		houses.push_back({ r, g, b });
	}
}

void DFS(int L, int cost, char ch)
{
	if (L == n - 1)
	{
		res = min(res, cost);
		return;
	}
	else
	{
		if (ch != 'R')
			DFS(L + 1, cost + houses[L + 1].r, 'R');
		if (ch != 'G')
			DFS(L + 1, cost + houses[L + 1].g, 'G');
		if (ch != 'B')
			DFS(L + 1, cost + houses[L + 1].b, 'B');
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "rt", stdin);

	setting();
	DFS(0, houses[0].r, 'R');
	DFS(0, houses[0].g, 'G');
	DFS(0, houses[0].b, 'B');

	cout << res;

	return 0;
}

DP๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int house[1001][3];
int N;
int cost[3];
void solve()
{
    house[0][0] = 0;
    house[0][1] = 0;
    house[0][2] = 0;
    cin >> N;
    for (int i = 1; i <= N; ++i)
    {
        cin >> cost[0] >> cost[1] >> cost[2];
        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][1], house[i - 1][0]) + cost[2];
    }
    cout << min(house[N][2], min(house[N][0], house[N][1]));
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen("input.txt", "rt", stdin);

    solve();
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: