본문 바로가기
Algorithm/백준

[백준] RGB거리 1149번 -java

by jackWillow 2021. 11. 7.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int N = Integer.parseInt(br.readLine());
		int[][] prices = new int[N][3];
		
		StringTokenizer st = null;
		for(int i = 0; i < N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			prices[i][0] = Integer.parseInt(st.nextToken());
			prices[i][1] = Integer.parseInt(st.nextToken());
			prices[i][2] = Integer.parseInt(st.nextToken());
		}
		for(int i = 1; i < N; i++) {
			for(int j = 0; j < 3; j++) {
				int preMax = prices[i-1][(j+1)%3] < prices[i-1][(j+2)%3] ? prices[i-1][(j+1)%3] : prices[i-1][(j+2)%3];
				prices[i][j] = preMax + prices[i][j];
			}
		}
		int ret = Math.min(prices[N-1][0], prices[N-1][1]);
		ret = Math.min(ret, prices[N-1][2]);
		System.out.println(ret);
		br.close();
	}
}
반응형