-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1149.java
More file actions
84 lines (76 loc) · 2.59 KB
/
1149.java
File metadata and controls
84 lines (76 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int dp[][]=new int[n][3];
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(br.readLine()," ");
int R=Integer.parseInt(st.nextToken());
int G=Integer.parseInt(st.nextToken());
int B=Integer.parseInt(st.nextToken());
if(i==0) {
dp[0][0]=R;
dp[0][1]=G;
dp[0][2]=B;
}
else {
dp[i][0]=Math.min(dp[i-1][1],dp[i-1][2])+R;//빨간색일 경우
dp[i][1]=Math.min(dp[i-1][0],dp[i-1][2])+G;//초록색일 경우
dp[i][2]=Math.min(dp[i-1][0],dp[i-1][1])+B;//파란색일 경우
}
}
int min=dp[n-1][0];
for(int i=1; i<3; i++) {
if(min>dp[n-1][i])
min=dp[n-1][i];
}
System.out.println(min);
}
}
/* 처음 틀렸을 때 코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static int min(int past,int arr[]){
int min = 1000;
int index=-1;
for(int i=0; i<3; i++){
if(min>arr[i]&&i!=past) {
index = i;
min = arr[i];
}
}
return index;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int color[][]=new int[n][3];
int house[] = new int[n];
int min=1000;
for(int i=0; i<n; i++){
StringTokenizer st = new StringTokenizer(br.readLine()," ");
color[i][0]=Integer.parseInt(st.nextToken());
color[i][1]=Integer.parseInt(st.nextToken());
color[i][2]=Integer.parseInt(st.nextToken());
}
house[0]=min(-1,color[0]);
for(int i=1; i<n; i++){
house[i]=min(house[i-1],color[i]);
}
int sum=0;
for(int i=0; i<n; i++){
sum+=color[i][house[i]];
}
for(int i=0; i<n; i++)
System.out.println(house[i]);
System.out.println(sum);
}
}
*/