My Blog

[JAVA] 1149: RGB거리 본문

알고리즘/백준

[JAVA] 1149: RGB거리

JAESG 2023. 2. 6. 12:55
package Baekjoon.DP;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class _1149 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int[][] data = new int[N+1][3];
        int[][] dp = new int[N+1][3];


        for (int i = 1; i <= N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            for (int j = 0; j < 3; j++) {
                data[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        dp[1][0] = data[1][0];
        dp[1][1] = data[1][1];
        dp[1][2] = data[1][2];

        int answer = 0;
        for(int i=1; i<=N; i++){
            dp[i][0] = Math.min(dp[i-1][1],dp[i-1][2]) + data[i][0];
            dp[i][1] = Math.min(dp[i-1][0],dp[i-1][2]) + data[i][1];
            dp[i][2] = Math.min(dp[i-1][0],dp[i-1][1]) + data[i][2];
            answer = Arrays.stream(dp[i]).min().getAsInt();
        }
        System.out.println(answer);
    }
}
728x90

'알고리즘 > 백준' 카테고리의 다른 글

[JAVA] 7576번: 토마토  (0) 2023.02.04
[JAVA] 7569번: 토마토  (0) 2023.02.04
자바 특수문자 split 함수 나누는 법  (0) 2023.01.17
Comments