250416 RGB거리

Jongleee·2025년 4월 16일
0

TIL

목록 보기
870/970
private static final int RED = 0;
private static final int GREEN = 1;
private static final int BLUE = 2;

public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int houseCount = Integer.parseInt(br.readLine());
	int[][] cost = readCostArray(br, houseCount);
	calculateMinCost(cost, houseCount);
	System.out.println(getMinFinalCost(cost, houseCount));
}

private static int[][] readCostArray(BufferedReader br, int houseCount) throws IOException {
	int[][] cost = new int[houseCount][3];
	for (int i = 0; i < houseCount; i++) {
		StringTokenizer st = new StringTokenizer(br.readLine());
		cost[i][RED] = Integer.parseInt(st.nextToken());
		cost[i][GREEN] = Integer.parseInt(st.nextToken());
		cost[i][BLUE] = Integer.parseInt(st.nextToken());
	}
	return cost;
}

private static void calculateMinCost(int[][] cost, int houseCount) {
	for (int i = 1; i < houseCount; i++) {
		cost[i][RED] += Math.min(cost[i - 1][GREEN], cost[i - 1][BLUE]);
		cost[i][GREEN] += Math.min(cost[i - 1][RED], cost[i - 1][BLUE]);
		cost[i][BLUE] += Math.min(cost[i - 1][RED], cost[i - 1][GREEN]);
	}
}

private static int getMinFinalCost(int[][] cost, int houseCount) {
	int last = houseCount - 1;
	return Math.min(Math.min(cost[last][RED], cost[last][GREEN]), cost[last][BLUE]);
}

출처:https://www.acmicpc.net/problem/1149

0개의 댓글