Problem Solving/BOJ

[Silver 1] 1149번 RGB거리

kmkunk 2022. 3. 29. 18:24
import java.io.*;

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[1001][4];
        int ans = 0;
        
        for(int i=1; i<=n; i++) {
            String[] arr = br.readLine().split(" ");
            int arr1 = Integer.parseInt(arr[0]);
            int arr2 = Integer.parseInt(arr[1]);
            int arr3 = Integer.parseInt(arr[2]);
            
            dp[i][1] = Math.min(dp[i-1][2], dp[i-1][3])+arr1;
            dp[i][2] = Math.min(dp[i-1][1], dp[i-1][3])+arr2;
            dp[i][3] = Math.min(dp[i-1][1], dp[i-1][2])+arr3;
        }
            
        ans = Math.min(dp[n][1], Math.min(dp[n][2], dp[n][3]));
        System.out.println(ans);
    }
}