IT 정보/알고리즘(백준, BOJ)

[백준-BOJ] 1149

Dalyoung 2017. 1. 20. 22:42
반응형

www.acmicpc.net/problem/1149

 

1149번: RGB거리

첫째 줄에 집의 수 N(2 ≤ N ≤ 1,000)이 주어진다. 둘째 줄부터 N개의 줄에는 각 집을 빨강, 초록, 파랑으로 칠하는 비용이 1번 집부터 한 줄에 하나씩 주어진다. 집을 칠하는 비용은 1,000보다 작거나

www.acmicpc.net

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		Main m = new Main();
		m.doit();
	}
	
	
	int N;
	int rgb[][] = new int[1000][3];
	
	int memo[][] = new int[1000][3];
	
	
	public void doit() throws FileNotFoundException{
		//System.setIn(new FileInputStream("input.txt"));
		Scanner sc = new Scanner(System.in);
		
		N = sc.nextInt();
		for(int i = 0; i < N; i++){
			for(int j = 0; j < 3; j++){
				rgb[i][j] = sc.nextInt();
				memo[i][j] = -1;
			}
		}
		int min = Math.min(gogo(0, 1), gogo(0, 0));
		min = Math.min(min, gogo(0, 2));
		System.out.println(min);
//		initMemo();
		sc.close();
	}
	
	int gogo(int y, int x){
		if(y >= N){
			return 0;
		}
		//y %= N;
		x %= 3;
		int ret = memo[y][x]; 
		if(ret != -1){
			return ret;
		}
		ret = Math.min(gogo(y+1, x+1), gogo(y+1, x+2)) + rgb[y][x];
		memo[y][x] = ret;
		//System.out.println(ret);
		return ret;
	}
}
반응형

'IT 정보 > 알고리즘(백준, BOJ)' 카테고리의 다른 글

[백준-BOJ] 1260  (0) 2017.01.20
[백준-BOJ] 1202  (0) 2017.01.20
[백준-BOJ] 1120  (0) 2017.01.14
[백준-BOJ] 1110  (0) 2017.01.14
[백준-BOJ] 1085  (0) 2017.01.14