Skip to content

Commit cfbf9df

Browse files
authored
[20250912] BOJ / G4 / RGB거리 2 / 한종욱
1 parent 4d379df commit cfbf9df

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static final int INF = 1000010;
9+
private static int[][] cost, dp;
10+
private static int N;
11+
12+
public static void main(String[] args) throws IOException {
13+
init();
14+
15+
int answer = Integer.MAX_VALUE;
16+
for (int i = 0; i < 3; i++) {
17+
DP(i);
18+
19+
for (int j = 0; j < 3; j++) {
20+
answer = Math.min(answer, dp[N-1][j]);
21+
}
22+
}
23+
24+
bw.write(answer + "\n");
25+
bw.flush();
26+
bw.close();
27+
br.close();
28+
29+
}
30+
31+
private static void init() throws IOException {
32+
N = Integer.parseInt(br.readLine());
33+
34+
cost = new int[N][3];
35+
dp = new int[N][3];
36+
37+
for (int i = 0; i < N; i++) {
38+
StringTokenizer st = new StringTokenizer(br.readLine());
39+
for (int j = 0; j < 3; j++) {
40+
cost[i][j] = Integer.parseInt(st.nextToken());
41+
}
42+
}
43+
}
44+
45+
private static void DP(int start) {
46+
for (int i = 0; i < N; i++) Arrays.fill(dp[i], INF);
47+
48+
dp[0][start] = cost[0][start];
49+
50+
for (int i = 1; i < N-1; i++) {
51+
dp[i][0] = cost[i][0] + Math.min(dp[i-1][1], dp[i-1][2]);
52+
dp[i][1] = cost[i][1] + Math.min(dp[i-1][0], dp[i-1][2]);
53+
dp[i][2] = cost[i][2] + Math.min(dp[i-1][0], dp[i-1][1]);
54+
}
55+
56+
for (int i = 0; i < 3; i++) {
57+
if (i == start) continue;
58+
59+
int min = INF;
60+
61+
for (int j = 0; j < 3; j++) {
62+
if (i == j) continue;
63+
min = Math.min(min, dp[N-2][j]);
64+
}
65+
66+
dp[N-1][i] = cost[N-1][i] + min;
67+
}
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)