본문 바로가기

백준

[백준] 2096 내려가기 [자바]

 

 


https://steady-coding.tistory.com/154

 

[BOJ] 백준 2096번 : 내려가기 (JAVA)

문제 N줄에 0 이상 9 이하의 숫자가 세 개씩 적혀 있다. 내려가기 게임을 하고 있는데, 이 게임은 첫 줄에서 시작해서 마지막 줄에서 끝나게 되는 놀이이다. 먼저 처음에 적혀 있는 세 개의 숫자

steady-coding.tistory.com

 

 


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


public class Main {
	static int dp[];
	static int min_dp[];
	static int[][] rope;
	static int N;
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		N = Integer.parseInt(br.readLine());
		dp = new int[3];
		min_dp = new int[3];
		rope = new int[N][3];
		for(int i = 0; i < N; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine(), " ");
			for(int j = 0; j < 3; j++) {
				rope[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		func();
		int result = Math.max(Math.max(dp[0], dp[1]), dp[2]);
		int minr = Math.min(Math.min(min_dp[0], min_dp[1]), min_dp[2]);
		System.out.println(result+" "+minr);
	}
	static int func() {
		dp[0] = min_dp[0] = rope[0][0];
		dp[1] = min_dp[1] = rope[0][1];
		dp[2] = min_dp[2] = rope[0][2];
		for(int i = 1; i < N; i++) {
			int one = dp[0];
			int two = dp[1];
			int mino = min_dp[0];
			int mint = min_dp[1];
			dp[0] = Math.max(dp[0], dp[1]) + rope[i][0];
			min_dp[0] = Math.min(min_dp[0], min_dp[1]) + rope[i][0];
			
			dp[1] = Math.max(Math.max(one, dp[2]), dp[1]) + rope[i][1];
			min_dp[1] = Math.min(Math.min(mino, min_dp[2]), min_dp[1]) + rope[i][1];
			
			dp[2] = Math.max(two, dp[2]) + rope[i][2];
			min_dp[2] = Math.min(min_dp[2], mint) + rope[i][2];
		}
		return 0;
	}
}