Skip to content

Commit 30fa65b

Browse files
committed
[Silver I] Title: RGB거리, Time: 108 ms, Memory: 14696 KB -BaekjoonHub
1 parent 62cfa59 commit 30fa65b

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

백준/Silver/1149. RGB거리/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 14672 KB, 시간: 112 ms
7+
메모리: 14696 KB, 시간: 108 ms
88

99
### 분류
1010

1111
다이나믹 프로그래밍
1212

1313
### 제출 일자
1414

15-
2024년 10월 21일 15:23:37
15+
2025년 8월 3일 19:26:02
1616

1717
### 문제 설명
1818

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
import java.io.*;
22
import java.util.*;
3+
34
public class Main {
4-
static int n;
5-
static int[][] map, dp;
6-
static final int max = 1000000;
5+
static int[][] house, dp;
6+
static int n, ans;
77
static StringTokenizer st;
88
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9-
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
109
public static void main(String[] args) throws Exception{
11-
input();
12-
System.out.println(recur(0, 0));
10+
inputSetting();
11+
12+
System.out.println(subSet(0, 0));
1313
}
1414

15-
static int recur(int cnt, int pre){
16-
if(cnt == n){
17-
return 0;
18-
}
19-
if(dp[cnt][pre] != 1000) return dp[cnt][pre];
20-
int rot = max;
21-
for(int i = 1; i < 4; i++) {
22-
if(i == pre) continue;
23-
rot = Math.min(rot, recur(cnt + 1, i) + map[cnt][i]);
15+
static int subSet(int cnt, int pre){
16+
if(cnt == n) return 0;
17+
if(dp[cnt][pre] != Integer.MAX_VALUE) return dp[cnt][pre];
18+
19+
int rot = Integer.MAX_VALUE;
20+
for(int i = 1; i < 4; i++){
21+
if(pre == i) continue;
22+
23+
dp[cnt][pre] = Math.min(subSet(cnt + 1, i) + house[cnt][i], dp[cnt][pre]);
2424
}
25-
return dp[cnt][pre] = rot;
25+
return dp[cnt][pre];
2626
}
2727

28-
static void input() throws Exception{
28+
static void inputSetting() throws Exception{
2929
n = Integer.parseInt(br.readLine());
30+
house = new int[n][4];
31+
dp = new int[n + 1][4];
32+
ans = Integer.MAX_VALUE;
3033

31-
map = new int[n][4];
32-
dp = new int[n][4];
3334
for(int i = 0; i < n; i++){
3435
st = new StringTokenizer(br.readLine());
3536

36-
Arrays.fill(dp[i], 1000);
37-
for(int j = 1; j < 4; j++){
38-
map[i][j] = Integer.parseInt(st.nextToken());
39-
}
37+
for(int j = 1; j < 4; j++) house[i][j] = Integer.parseInt(st.nextToken());
38+
Arrays.fill(dp[i], Integer.MAX_VALUE);
4039
}
41-
40+
Arrays.fill(dp[n], Integer.MAX_VALUE);
4241
}
4342
}

0 commit comments

Comments
 (0)