Lewis's Tech Keep
[백준] RGB 거리 본문
참고 : www.acmicpc.net/problem/1149
1149번: RGB거리
첫째 줄에 집의 수 N(2 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 각 집을 빨강, 초록, 파랑으로 칠하는 비용이 1번 집부터 한 줄에 하나씩 주어진다. 집을 칠하는 비용은 1,000보다 작거나
www.acmicpc.net
- DP 문제
- dp[i][0~2] : 각 색깔별 i번 째에서의 최소 비용 (단, 0는 red, 1은 green, 2는 blue를 무조건 선택하는 경우의 수)
더보기
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[][] s = new int[1005][3];
int[][] dp = new int[1005][3];
for(int i = 1; i<=n; i++) {
String[] input = sc.nextLine().split(" ");
s[i][0] = Integer.parseInt(input[0]);
s[i][1] = Integer.parseInt(input[1]);
s[i][2] = Integer.parseInt(input[2]);
}
dp[1][0] = s[1][0];// red
dp[1][1] = s[1][1];// green
dp[1][2] = s[1][2];// blue
for(int i=2; i<=n; i++) {
dp[i][0] = Math.min(dp[i-1][1], dp[i-1][2]) + s[i][0];
dp[i][1] = Math.min(dp[i-1][0], dp[i-1][2]) + s[i][1];
dp[i][2] = Math.min(dp[i-1][0], dp[i-1][1]) + s[i][2];
}
System.out.println(Math.min(dp[n][0], Math.min(dp[n][1], dp[n][2])));
}
}
'Java > 알고리즘' 카테고리의 다른 글
[백준] 1로 만들기 -2 (0) | 2021.03.06 |
---|---|
[백준] 구간 합 구하기 4 (0) | 2021.03.06 |
[백준] 계단 오르기 (0) | 2021.03.05 |
[백준] 1, 2, 3 더하기 (0) | 2021.03.05 |
[백준] 1로 만들기 (0) | 2021.03.04 |
Comments