From bfc0486ba457b408b76282173520d3c50b422e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=A7=84?= Date: Mon, 29 Sep 2025 17:04:00 +0900 Subject: [PATCH] =?UTF-8?q?[20250929]=20BOJ=20/=20G4=20/=20RGB=EA=B1=B0?= =?UTF-8?q?=EB=A6=AC=202=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...9 BOJ G4 RGB\352\261\260\353\246\254 2.md" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "zinnnn37/202509/29 BOJ G4 RGB\352\261\260\353\246\254 2.md" diff --git "a/zinnnn37/202509/29 BOJ G4 RGB\352\261\260\353\246\254 2.md" "b/zinnnn37/202509/29 BOJ G4 RGB\352\261\260\353\246\254 2.md" new file mode 100644 index 00000000..ee698067 --- /dev/null +++ "b/zinnnn37/202509/29 BOJ G4 RGB\352\261\260\353\246\254 2.md" @@ -0,0 +1,63 @@ +```java +import java.io.*; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class BJ_17404_RGB거리_2 { + + private static final int INF = 987654321; + + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, ans; + private static int[][] colors; + private static int[][] dp; + + public static void main(String[] args) throws IOException { + init(); + sol(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + ans = INF; + + colors = new int[N][3]; + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < 3; j++) { + colors[i][j] = Integer.parseInt(st.nextToken()); + } + } + dp = new int[N][3]; + } + + private static void sol() throws IOException { + // 시작 색 지정 + for (int firstColor = 0; firstColor < 3; firstColor++) { + Arrays.fill(dp[0], INF); + + dp[0][firstColor] = colors[0][firstColor]; + + for (int i = 1; i < N; i++) { + dp[i][0] = Math.min(dp[i - 1][1], dp[i - 1][2]) + colors[i][0]; + dp[i][1] = Math.min(dp[i - 1][0], dp[i - 1][2]) + colors[i][1]; + dp[i][2] = Math.min(dp[i - 1][0], dp[i - 1][1]) + colors[i][2]; + } + + for (int lastColor = 0; lastColor < 3; lastColor++) { + if (lastColor != firstColor) { + ans = Math.min(ans, dp[N - 1][lastColor]); + } + } + } + bw.write(ans + ""); + bw.flush(); + bw.close(); + br.close(); + } + +} +``` \ No newline at end of file