File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 문제
2
+ RGB 거리
3
+ ## 문제 원본
4
+ 문제의 원본은 [ 여기서] ( https://www.acmicpc.net/problem/1149 ) 확인하세요.
5
+
6
+ ## 분류
7
+ * 다이나믹 프로그래밍
8
+
9
+ # 풀이
10
+
11
+ n번쨰 집에서 선택된 색이 아닌 n - 1번째 집의 최소 누적합을 가지는 색의 값에 n번째 집에서 칠할 색의 비용을 더한다.
12
+
13
+ ```
14
+ dp[0][R] = rgb[0][R];
15
+ dp[0][G] = rgb[0][G];
16
+ dp[0][B] = rgb[0][B];
17
+
18
+ dp[i][R] = min(dp[i - 1][G], dp[i - 1][B]) + rgb[i][R];
19
+ dp[i][G] = min(dp[i - 1][R], dp[i - 1][B]) + rgb[i][G];
20
+ dp[i][B] = min(dp[i - 1][R], dp[i - 1][G]) + rgb[i][B];
21
+ ```
22
+
23
+ 위의 점화식을 이용해 n번째 까지 구한다.
24
+
25
+ ``` c++
26
+ #include < iostream>
27
+ #include < vector>
28
+ #include < algorithm>
29
+
30
+ using namespace std ;
31
+
32
+ enum Color {
33
+ R = 0, G = 1, B = 2
34
+ };
35
+
36
+ int main (void) {
37
+ ios::sync_with_stdio(false);
38
+ cin.tie(0); cout.tie(0);
39
+
40
+ int n;
41
+ cin >> n;
42
+
43
+ int** rgb = new int* [n];
44
+ int** dp = new int* [n];
45
+
46
+ for (int i = 0; i < n; i++) {
47
+ rgb[i] = new int[3];
48
+ dp[i] = new int[3];
49
+ }
50
+
51
+
52
+ for (int i = 0; i < n; i++) {
53
+ for (int j = 0; j < 3; j++) {
54
+ int cost;
55
+ cin >> cost;
56
+
57
+ rgb[i][j] = cost;
58
+ }
59
+ }
60
+
61
+ dp[0][R] = rgb[0][R];
62
+ dp[0][G] = rgb[0][G];
63
+ dp[0][B] = rgb[0][B];
64
+
65
+ for (int i = 1; i < n; i++) {
66
+ dp[i][R] = min(dp[i - 1][G], dp[i - 1][B]) + rgb[i][R];
67
+ dp[i][G] = min(dp[i - 1][R], dp[i - 1][B]) + rgb[i][G];
68
+ dp[i][B] = min(dp[i - 1][R], dp[i - 1][G]) + rgb[i][B];
69
+ }
70
+
71
+
72
+ int result = dp[n - 1][R];
73
+ result = min(result, dp[n - 1][G]);
74
+ result = min(result, dp[n - 1][B]);
75
+
76
+ cout << result << endl;
77
+
78
+ return 0;
79
+ }
80
+ ```
You can’t perform that action at this time.
0 commit comments