Skip to content

Commit bc18397

Browse files
authored
Merge pull request #1846 from AlgorithmWithGod/ksinji
[20260127] BOJ / G4 / RGB거리 2 / 강신지
2 parents 8d7b34d + d84000a commit bc18397

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int n;
7+
static int[][] cost;
8+
static int[][] dp;
9+
10+
static final int INF = 1_000_000_000;
11+
12+
public static void main(String[] args) throws Exception {
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
StringTokenizer st;
15+
16+
n = Integer.parseInt(br.readLine());
17+
cost = new int[n][3];
18+
19+
for (int i = 0; i < n; i++) {
20+
st = new StringTokenizer(br.readLine());
21+
cost[i][0] = Integer.parseInt(st.nextToken());
22+
cost[i][1] = Integer.parseInt(st.nextToken());
23+
cost[i][2] = Integer.parseInt(st.nextToken());
24+
}
25+
26+
int ans = INF;
27+
28+
for (int firstColor = 0; firstColor < 3; firstColor++) {
29+
dp = new int[n][3];
30+
31+
for (int c = 0; c < 3; c++) {
32+
if (c == firstColor) {
33+
dp[0][c] = cost[0][c];
34+
} else {
35+
dp[0][c] = INF;
36+
}
37+
}
38+
39+
for (int i = 1; i < n; i++) {
40+
dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + cost[i][0];
41+
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + cost[i][1];
42+
dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + cost[i][2];
43+
}
44+
45+
for (int lastColor = 0; lastColor < 3; lastColor++) {
46+
if (lastColor != firstColor) {
47+
ans = Math.min(ans, dp[n - 1][lastColor]);
48+
}
49+
}
50+
}
51+
52+
System.out.println(ans);
53+
```
54+
}
55+
}

0 commit comments

Comments
 (0)