Skip to content

Instantly share code, notes, and snippets.

@youngvctr
Created July 29, 2023 08:26
Show Gist options
  • Save youngvctr/74d1f4b0dc911f1a6e9d5759a39acee6 to your computer and use it in GitHub Desktop.
Save youngvctr/74d1f4b0dc911f1a6e9d5759a39acee6 to your computer and use it in GitHub Desktop.
BOJ | DP[1149] > RGB거리
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int[][] expense = new int[N + 1][3];
int[][] house = new int[N + 1][3];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 3; j++) {
house[i][j] = Integer.parseInt(st.nextToken());
}
}
for (int i = 1; i <= N; i++) {
expense[i][0] = Math.min(expense[i - 1][1], expense[i - 1][2]) + house[i][0]; // R
expense[i][1] = Math.min(expense[i - 1][0], expense[i - 1][2]) + house[i][1]; // G
expense[i][2] = Math.min(expense[i - 1][0], expense[i - 1][1]) + house[i][2]; // B
}
System.out.println(Math.min(Math.min(expense[N][0], expense[N][1]), expense[N][2]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment