[BOJ]1149 RGB거리.java

전영서·2021년 9월 14일
0

Algorithm

목록 보기
38/89

1.문제

2.코드

package prac;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;

/**
 * Author : YoungSeo Jeon
 * Date : 2021-09-14
 * Description : 백준 
 */


public class Main{
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int N = Integer.parseInt(br.readLine());
		
		int[][] cost = new int[N+1][3];
		
		StringTokenizer st;
		for(int i=0; i<N; i++) {
			st = new StringTokenizer(br.readLine());
			
			for(int j=0; j<3; j++) {
				cost[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		for(int i=1; i<=N; i++) {
			for(int j=0; j<3; j++) {
				cost[i][j] += Math.min(cost[i-1][(j+1)%3],cost[i-1][(j+2)%3]);
			}
		}
		
		int result = 987654321;
		
		for(int i=0; i<3; i++) {
			result = Math.min(result, cost[N-1][i]);
		}
		
		bw.write(result+"\n");
		bw.flush();
		bw.close();
		br.close();
		
	}
}

3.Review

위에서부터 하던지 아래서부터 하던지 상관은 없다

진행하면서 칠할수있는 가장 작은 cost를 더해주면서 진행해나간다면

최솟값을 찾을수 잇다.

profile
꾸준히 성실하게

0개의 댓글