From 203e7ce1e5706c1eecc86c8f01250b9c876fec09 Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Tue, 7 Oct 2025 14:23:51 +0900 Subject: [PATCH] =?UTF-8?q?[20251007]=20BOJ=20/=20G4=20/=20=EC=9A=B0?= =?UTF-8?q?=EC=9C=A0=20=EB=8F=84=EC=8B=9C=20/=20=ED=95=9C=EC=A2=85?= =?UTF-8?q?=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\234\240 \353\217\204\354\213\234.md" | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 "Ukj0ng/202510/07 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" diff --git "a/Ukj0ng/202510/07 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" "b/Ukj0ng/202510/07 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" new file mode 100644 index 00000000..d2feda55 --- /dev/null +++ "b/Ukj0ng/202510/07 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" @@ -0,0 +1,84 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static final int[] dx = {1, 0}; + private static final int[] dy = {0, 1}; + private static int[][] map; + private static int[][][] dp; + private static int N; + + public static void main(String[] args) throws IOException { + init(); + DP(); + + int answer = 0; + for (int i = 0; i < 3; i++) { + answer = Math.max(answer, dp[N-1][N-1][i]); + } + bw.write(answer + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + N = Integer.parseInt(br.readLine()); + + map = new int[N][N]; + dp = new int[N][N][3]; + + for (int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + Arrays.fill(dp[i][j], -1); + } + } + } + + private static void DP() { + if (map[0][0] == 0) dp[0][0][0] = 1; + dp[0][0][2] = 0; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + for (int k = 0; k < 3; k++) { + if (dp[i][j][k] == -1) continue; + + for (int l = 0; l < 2; l++) { + int nx = i + dx[l]; + int ny = j + dy[l]; + + if (OOB(nx, ny)) continue; + + dp[nx][ny][k] = Math.max(dp[nx][ny][k], dp[i][j][k]); + + int next = (k+1)%3; + if (map[nx][ny] == next) { + dp[nx][ny][next] = Math.max(dp[nx][ny][next], dp[i][j][k]+1); + } + } + } + } + } + } + + private static boolean OOB(int nx, int ny) { + return nx < 0 || nx > N-1 || ny < 0 || ny > N-1; + } + + private static boolean canDrink(int current, int next) { + if (current == 0 && next == 1) return true; + + if (current == 1 && next == 2) return true; + + if (current == 2 && next == 0) return true; + + return false; + } +} +```