From 83a8ae05a1c1661e873cc8516fb29509d64543f4 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Tue, 19 Aug 2025 13:12:30 +0900 Subject: [PATCH] =?UTF-8?q?[20250819]=20BOJ=20/=20G2=20/=20=EB=A1=9C?= =?UTF-8?q?=EB=B4=87=20=EC=A1=B0=EC=A2=85=ED=95=98=EA=B8=B0=20/=20?= =?UTF-8?q?=EC=9D=B4=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\242\205\355\225\230\352\270\260 .md" | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 "0224LJH/202508/19 BOJ \353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260 .md" diff --git "a/0224LJH/202508/19 BOJ \353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260 .md" "b/0224LJH/202508/19 BOJ \353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260 .md" new file mode 100644 index 00000000..ecb1db9c --- /dev/null +++ "b/0224LJH/202508/19 BOJ \353\241\234\353\264\207 \354\241\260\354\242\205\355\225\230\352\270\260 .md" @@ -0,0 +1,98 @@ +```java + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.StringTokenizer; + +public class Main { + + static int height,width,ans; + + static int[][] arr; + static int[][][] dp; + + static final int TOP = 0; + static final int LEFT = 1; + static final int RIGHT = 2; + static final int MIN = Integer.MIN_VALUE/2; + + public static void main(String[] args) throws IOException { + init(); + process(); + print(); + } + + private static void init() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + height = Integer.parseInt(st.nextToken()); + width = Integer.parseInt(st.nextToken()); + arr = new int[height][width]; + dp = new int[height][width][3]; + ans = MIN; + // 합의 최댓값을 직전에 어느 방향에서 왔는지에 따라 구분. + // 0 ,1 , 2가 순서대로 위,왼,오 + + for (int i = 0; i < height; i++){ + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < width; j++){ + arr[i][j] = Integer.parseInt(st.nextToken()); + Arrays.fill(dp[i][j], MIN); + } + } + + } + + private static void process() throws IOException { + calculateFirstLine(); + for (int i = 1; i < height; i++){ + calculateFromTop(i); + calculateFromLeft(i); + calculateFromRight(i); + } + + for (int i = 0; i < 3; i++){ + ans = Math.max (ans,dp[height-1][width-1][i]); + } + + } + + private static void calculateFromRight(int curHeight) { + dp[curHeight][width-1][RIGHT] = dp[curHeight][width-1][TOP]; + for (int i = width - 2; i >= 0; i--) { + dp[curHeight][i][RIGHT] = arr[curHeight][i] + Math.max(dp[curHeight][i+1][TOP], dp[curHeight][i+1][RIGHT]); + } + } + + private static void calculateFromLeft(int curHeight) { + dp[curHeight][0][LEFT] = dp[curHeight][0][TOP]; + for (int i = 1; i < width; i++){ + dp[curHeight][i][LEFT] = arr[curHeight][i] + Math.max(dp[curHeight][i-1][TOP], dp[curHeight][i-1][LEFT]) ; + } + } + + private static void calculateFromTop(int curHeight) { + for (int i = 0; i < width; i++){ + int curLargest = MIN; + for (int j = 0; j < 3; j++) curLargest = Math.max(curLargest, dp[curHeight-1][i][j]); + + dp[curHeight][i][TOP] = curLargest + arr[curHeight][i]; + } + } + + private static void calculateFirstLine() { + int cur = 0; + for (int i = 0; i < width; i++){ + cur += arr[0][i]; + dp[0][i][0] = cur; + } + } + + + private static void print() { + System.out.println(ans); + } +} +``` \ No newline at end of file