
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
int[][] price = new int[N][3];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
price[i][0] = Integer.parseInt(st.nextToken());
price[i][1] = Integer.parseInt(st.nextToken());
price[i][2] = Integer.parseInt(st.nextToken());
}
br.close();
int[][] dp = new int[N][3];
dp[0][0] = price[0][0]; // r
dp[0][1] = price[0][1]; // g
dp[0][2] = price[0][2]; // b
for (int i = 1; i < N; i++) {
dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + price[i][0];
dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + price[i][1];
dp[i][2] = Math.min(dp[i - 1][1], dp[i - 1][0]) + price[i][2];
}
int min = Math.min(dp[N - 1][0], Math.min(dp[N - 1][1], dp[N - 1][2]));
bw.write(Integer.toString(min));
bw.close();
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 1932번: 정수 삼각형 (0) | 2025.04.14 |
|---|---|
| [JAVA-자바] 1629번: 곱셈 (0) | 2025.04.11 |
| [JAVA-자바] 16953번: A → B (0) | 2025.04.10 |
| [JAVA-자바] 15666번: N과 M (12) (0) | 2025.04.10 |
| [JAVA-자바] 15663번: N과 M (9) (0) | 2025.04.10 |