From f7921c7d942434e683322a14f7d1191615253f57 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Wed, 12 Nov 2025 23:42:41 +0900 Subject: [PATCH] =?UTF-8?q?[20251112]=20BOJ=20/=20G5=20/=20=EB=B3=B4?= =?UTF-8?q?=EB=AC=BC=EC=84=AC=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...64\353\254\274\354\204\254.md\342\200\216" | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 "Seol-JY/202511/12 BOJ G5 \353\263\264\353\254\274\354\204\254.md\342\200\216" diff --git "a/Seol-JY/202511/12 BOJ G5 \353\263\264\353\254\274\354\204\254.md\342\200\216" "b/Seol-JY/202511/12 BOJ G5 \353\263\264\353\254\274\354\204\254.md\342\200\216" new file mode 100644 index 00000000..9097256a --- /dev/null +++ "b/Seol-JY/202511/12 BOJ G5 \353\263\264\353\254\274\354\204\254.md\342\200\216" @@ -0,0 +1,83 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static char[][] map; + static int[] dx = {-1, 1, 0, 0}; + static int[] dy = {0, 0, -1, 1}; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = new char[N][M]; + List lands = new ArrayList<>(); + + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + map[i][j] = line.charAt(j); + if (map[i][j] == 'L') { + lands.add(new Point(i, j)); + } + } + } + + int maxDistance = 0; + + for (Point land : lands) { + int distance = bfs(land.x, land.y); + maxDistance = Math.max(maxDistance, distance); + } + + System.out.println(maxDistance); + } + + static int bfs(int startX, int startY) { + ArrayDeque queue = new ArrayDeque<>(); + int[][] distance = new int[N][M]; + + for (int i = 0; i < N; i++) { + Arrays.fill(distance[i], -1); + } + + queue.offer(new Point(startX, startY)); + distance[startX][startY] = 0; + + int maxDist = 0; + + while (!queue.isEmpty()) { + Point current = queue.poll(); + + for (int i = 0; i < 4; i++) { + int nx = current.x + dx[i]; + int ny = current.y + dy[i]; + + if (nx >= 0 && nx < N && ny >= 0 && ny < M) { + if (map[nx][ny] == 'L' && distance[nx][ny] == -1) { + distance[nx][ny] = distance[current.x][current.y] + 1; + maxDist = Math.max(maxDist, distance[nx][ny]); + queue.offer(new Point(nx, ny)); + } + } + } + } + + return maxDist; + } + + static class Point { + int x, y; + + Point(int x, int y) { + this.x = x; + this.y = y; + } + } +} +```