백준 17404 c++ : DP

magicdrill·2024년 12월 12일

백준 문제풀이

목록 보기
506/673

백준 17404 c++ : DP

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

void input_RGB(vector<vector<int>> &RGB) {
	cout << "\ninput_RGB()\n";

	int i, j, N;
	int R, G, B;
	cin >> N;
	for (i = 0; i < N; i++) {
		cin >> R >> G >> B;
		RGB.push_back({ R, G, B });
	}

	return;
}

int find_answer(vector<vector<int>>& RGB) {
	cout << "\nfind_answer()\n";
	int answer = 2100000000;
	int i, j, N = RGB.size();
	vector<vector<int>>DP(N, vector<int>(3));

	//모든 집을 칠하는 비용의 최솟값
	//전체 비용 - 최대비용?

	//최초 색깔 지정
	for (i = 0; i < 3; i++) {

		//1번 집의 색깔 지정
		for (j = 0; j < 3; j++) {
			if (i == j) {
				DP[0][j] = RGB[0][j];
			}
			else {
				DP[0][j] = 2100000000;
			}
		}

		//2번부터 N까지의 집 색깔 지정
		for (j = 1; j < N; j++) {
			DP[j][0] = RGB[j][0] + min(DP[j-1][1], DP[j-1][2]);
			DP[j][1] = RGB[j][1] + min(DP[j - 1][0], DP[j - 1][2]);
			DP[j][2] = RGB[j][2] + min(DP[j - 1][0], DP[j - 1][1]);
		}

		//1번과 다른 N번 집 색깔 찾기
		for (j = 0; j < 3; j++) {
			if (j == i) {
				continue;
			}
			else {
				answer = min(answer, DP[N-1][j]);
			}
		}

		cout << "첫 집의 색은 " << i << "번 색상을 고름\n";
		for (j = 0; j < DP.size(); j++) {
			cout << DP[j][0] << " " << DP[j][1] << " " << DP[j][2] << "\n";
		}
		cout << "\n";
	}

	return answer;
}

int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	vector<vector<int>> RGB;

	input_RGB(RGB);
	cout << find_answer(RGB) << "\n";

	return 0;
}

0개의 댓글