-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Gold IV] Title: RGB거리 2, Time: 0 ms, Memory: 2168 KB -BaekjoonHub
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# [Gold IV] RGB거리 2 - 17404 | ||
|
||
[문제 링크](https://www.acmicpc.net/problem/17404) | ||
|
||
### 성능 요약 | ||
|
||
메모리: 2168 KB, 시간: 0 ms | ||
|
||
### 분류 | ||
|
||
다이나믹 프로그래밍 | ||
|
||
### 제출 일자 | ||
|
||
2024년 4월 28일 02:22:02 | ||
|
||
### 문제 설명 | ||
|
||
<p>RGB거리에는 집이 N개 있다. 거리는 선분으로 나타낼 수 있고, 1번 집부터 N번 집이 순서대로 있다.</p> | ||
|
||
<p>집은 빨강, 초록, 파랑 중 하나의 색으로 칠해야 한다. 각각의 집을 빨강, 초록, 파랑으로 칠하는 비용이 주어졌을 때, 아래 규칙을 만족하면서 모든 집을 칠하는 비용의 최솟값을 구해보자.</p> | ||
|
||
<ul> | ||
<li>1번 집의 색은 2번, N번 집의 색과 같지 않아야 한다.</li> | ||
<li>N번 집의 색은 N-1번, 1번 집의 색과 같지 않아야 한다.</li> | ||
<li>i(2 ≤ i ≤ N-1)번 집의 색은 i-1, i+1번 집의 색과 같지 않아야 한다.</li> | ||
</ul> | ||
|
||
### 입력 | ||
|
||
<p>첫째 줄에 집의 수 N(2 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 각 집을 빨강, 초록, 파랑으로 칠하는 비용이 1번 집부터 한 줄에 하나씩 주어진다. 집을 칠하는 비용은 1,000보다 작거나 같은 자연수이다.</p> | ||
|
||
### 출력 | ||
|
||
<p>첫째 줄에 모든 집을 칠하는 비용의 최솟값을 출력한다.</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <iostream> | ||
#include <algorithm> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
vector<vector<int>> rgb; | ||
|
||
int main() { | ||
ios_base::sync_with_stdio(0); | ||
cin.tie(0); | ||
|
||
int n; | ||
cin >> n; | ||
rgb.assign(n+1, vector<int>(3, 0)); | ||
vector<vector<int>> dp(n+1, vector<int>(3, 0)); | ||
|
||
for (int i = 1; i <= n; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
cin >> rgb[i][j]; | ||
} | ||
} | ||
|
||
int ans = 10000000; | ||
for (int k = 0; k < 3; k++) { | ||
for (int i = 0; i < 3; i++) { | ||
if (i == k) dp[1][i] = rgb[1][i]; | ||
else dp[1][i] = 10000000; | ||
} | ||
for (int i = 2; i <= n; i++) { | ||
dp[i][0] = min(dp[i-1][1], dp[i-1][2]) + rgb[i][0]; | ||
dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + rgb[i][1]; | ||
dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + rgb[i][2]; | ||
} | ||
for (int i = 0; i < 3; i++) { | ||
if (i != k) ans = min(ans, dp[n][i]); | ||
} | ||
} | ||
cout << ans; | ||
|
||
return 0; | ||
} |