From 87a6a16164179d53bdcb450e123b8710fc632b1f Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Mon, 30 Jun 2025 21:10:24 +0900 Subject: [PATCH] =?UTF-8?q?[20250630]=20BOJ=20/=20G4=20/=20RGB=EA=B1=B0?= =?UTF-8?q?=EB=A6=AC=202=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0 BOJ G4 RGB\352\261\260\353\246\254 2.md" | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 "Seol-JY/202506/30 BOJ G4 RGB\352\261\260\353\246\254 2.md" diff --git "a/Seol-JY/202506/30 BOJ G4 RGB\352\261\260\353\246\254 2.md" "b/Seol-JY/202506/30 BOJ G4 RGB\352\261\260\353\246\254 2.md" new file mode 100644 index 00000000..989fb6be --- /dev/null +++ "b/Seol-JY/202506/30 BOJ G4 RGB\352\261\260\353\246\254 2.md" @@ -0,0 +1,51 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static final int RED = 0; + static final int GREEN = 1; + static final int BLUE = 2; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int n = Integer.parseInt(br.readLine()); + + int[][] cost = new int[n][3]; + for (int i = 0; i < n; 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()); + } + + int answer = 987654321; + + for (int firstColor = 0; firstColor < 3; firstColor++) { + int[][] dp = new int[n][3]; + + for (int j = 0; j < 3; j++) { + if (j == firstColor) { + dp[0][j] = cost[0][j]; + } else { + dp[0][j] = 987654321; + } + } + + for (int i = 1; i < n; i++) { + dp[i][RED] = Math.min(dp[i-1][GREEN], dp[i-1][BLUE]) + cost[i][RED]; + dp[i][GREEN] = Math.min(dp[i-1][RED], dp[i-1][BLUE]) + cost[i][GREEN]; + dp[i][BLUE] = Math.min(dp[i-1][RED], dp[i-1][GREEN]) + cost[i][BLUE]; + } + + for (int j = 0; j < 3; j++) { + if (j != firstColor) { + answer = Math.min(answer, dp[n-1][j]); + } + } + } + + System.out.println(answer); + } +} +```