From de5689f8709c126a308b9a3a4f8a1dff8760afcd Mon Sep 17 00:00:00 2001 From: Ukj0ng <90972240+Ukj0ng@users.noreply.github.com> Date: Wed, 8 Oct 2025 12:41:13 +0900 Subject: [PATCH] =?UTF-8?q?[20251008]=20BOJ=20/=20G4=20/=20=EB=B0=B1?= =?UTF-8?q?=EB=A3=B8=20/=20=ED=95=9C=EC=A2=85=EC=9A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../08 BOJ G4 \353\260\261\353\243\270.md" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "Ukj0ng/202510/08 BOJ G4 \353\260\261\353\243\270.md" diff --git "a/Ukj0ng/202510/08 BOJ G4 \353\260\261\353\243\270.md" "b/Ukj0ng/202510/08 BOJ G4 \353\260\261\353\243\270.md" new file mode 100644 index 00000000..d0121d84 --- /dev/null +++ "b/Ukj0ng/202510/08 BOJ G4 \353\260\261\353\243\270.md" @@ -0,0 +1,81 @@ +``` +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 int[][] map, dp; + private static int[] wall; + private static boolean vertical; + private static int N, M; + + public static void main(String[] args) throws IOException { + init(); + DP(); + + if (dp[N-1][M-1] == -1000000001) bw.write("Entity"); + else bw.write(dp[N-1][M-1] + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = new int[N][M]; + dp = new int[N][M]; + wall = new int[4]; + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < M; j++) { + map[i][j] = Integer.parseInt(st.nextToken()); + dp[i][j] = -1000000001; + } + } + + st = new StringTokenizer(br.readLine()); + wall[0] = Integer.parseInt(st.nextToken()); + wall[1] = Integer.parseInt(st.nextToken()); + wall[2] = Integer.parseInt(st.nextToken()); + wall[3] = Integer.parseInt(st.nextToken()); + + vertical = wall[1] == wall[3]; + } + + private static void DP() { + dp[0][0] = map[0][0]; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + if (i == 0 && j == 0) continue; + + if (i > 0 && dp[i-1][j] != -1000000001 && canMove(i-1, j, 0)) { + dp[i][j] = Math.max(dp[i][j], dp[i-1][j] + map[i][j]); + } + + if (j > 0 && dp[i][j-1] != -1000000001 && canMove(i, j-1, 1)) { + dp[i][j] = Math.max(dp[i][j], dp[i][j-1] + map[i][j]); + } + } + } + } + + private static boolean canMove(int x, int y, int dir) { + if (vertical && dir == 1) { + int x_min = Math.min(wall[0], wall[2]); + int x_max = Math.max(wall[0], wall[2]); + if ((x_min <= x && x < x_max) && y+1 == wall[1]) return false; + } else if (!vertical && dir == 0) { + int y_min = Math.min(wall[1], wall[3]); + int y_max = Math.max(wall[1], wall[3]); + if (x+1 == wall[0] && (y_min <= y && y < y_max)) return false; + } + return true; + } +} +```