From 1476b668eb92c36f2aa2ee93947ecfb1c1856de8 Mon Sep 17 00:00:00 2001 From: JHLEE325 <82587652+JHLEE325@users.noreply.github.com> Date: Thu, 7 Aug 2025 17:59:57 +0900 Subject: [PATCH] =?UTF-8?q?[20250807]=20BOJ=20/=20G4=20/=20=ED=85=8C?= =?UTF-8?q?=ED=8A=B8=EB=A1=9C=EB=AF=B8=EB=85=B8=20/=20=EC=9D=B4=EC=A4=80?= =?UTF-8?q?=ED=9D=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...70\353\241\234\353\257\270\353\205\270.md" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "JHLEE325/202508/07 BOJ G4 \355\205\214\355\212\270\353\241\234\353\257\270\353\205\270.md" diff --git "a/JHLEE325/202508/07 BOJ G4 \355\205\214\355\212\270\353\241\234\353\257\270\353\205\270.md" "b/JHLEE325/202508/07 BOJ G4 \355\205\214\355\212\270\353\241\234\353\257\270\353\205\270.md" new file mode 100644 index 00000000..de3d431f --- /dev/null +++ "b/JHLEE325/202508/07 BOJ G4 \355\205\214\355\212\270\353\241\234\353\257\270\353\205\270.md" @@ -0,0 +1,83 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + static int n, m; + static int[][] board; + static boolean[][] visited; + static int max = 0; + + static final int[][] dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + st = new StringTokenizer(br.readLine()); + n = Integer.parseInt(st.nextToken()); + m = Integer.parseInt(st.nextToken()); + board = new int[n][m]; + visited = new boolean[n][m]; + + for (int r = 0; r < n; r++) { + st = new StringTokenizer(br.readLine()); + for (int c = 0; c < m; c++) { + board[r][c] = Integer.parseInt(st.nextToken()); + } + } + + for (int r = 0; r < n; r++) { + for (int c = 0; c < m; c++) { + visited[r][c] = true; + dfs(r, c, board[r][c], 1); + visited[r][c] = false; + + chkother(r, c); + } + } + + System.out.println(max); + } + + static void dfs(int x, int y, int sum, int depth) { + if (depth == 4) { + max = Math.max(max, sum); + return; + } + + for (int d = 0; d < 4; d++) { + int nx = x + dir[d][0]; + int ny = y + dir[d][1]; + + if (nx < 0 || ny < 0 || nx >= n || ny >= m || visited[nx][ny]) continue; + + visited[nx][ny] = true; + dfs(nx, ny, sum + board[nx][ny], depth + 1); + visited[nx][ny] = false; + } + } + + static void chkother(int x, int y) { + int wing = 0; + int min = Integer.MAX_VALUE; + int sum = board[x][y]; + + for (int d = 0; d < 4; d++) { + int nx = x + dir[d][0]; + int ny = y + dir[d][1]; + + if (nx < 0 || ny < 0 || nx >= n || ny >= m) continue; + wing++; + sum += board[nx][ny]; + min = Math.min(min, board[nx][ny]); + } + + if (wing < 3) return; + if (wing == 4) sum -= min; + max = Math.max(max, sum); + } +} + +```