From dc0b14711c4fa84483e1aff152c953b281b892d2 Mon Sep 17 00:00:00 2001 From: Jonghwan Lee <123362165+0224LJH@users.noreply.github.com> Date: Fri, 18 Jul 2025 21:58:15 +0900 Subject: [PATCH] =?UTF-8?q?[20250718]=20BOJ=20/=20G3=20/=20=EB=8B=A4?= =?UTF-8?q?=EB=A6=AC=20=EB=A7=8C=EB=93=A4=EA=B8=B0=20/=20=EC=9D=B4?= =?UTF-8?q?=EC=A2=85=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... \353\247\214\353\223\244\352\270\260 .md" | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 "0224LJH/202507/18 BOJ \353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260 .md" diff --git "a/0224LJH/202507/18 BOJ \353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260 .md" "b/0224LJH/202507/18 BOJ \353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260 .md" new file mode 100644 index 00000000..d6fdf3d8 --- /dev/null +++ "b/0224LJH/202507/18 BOJ \353\213\244\353\246\254 \353\247\214\353\223\244\352\270\260 .md" @@ -0,0 +1,130 @@ +```java +import java.awt.*; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static int[][] arr; + static boolean[][] visited; + static int[] dy = {-1,0,1,0}; + static int[] dx = {0,1,0,-1}; + static int size; + static final int LAND = 1; + static final int WATER = 0; + + public static void main(String[] args) throws IOException { + init(); + int result = process(); + System.out.println(result); + } + + private static void init() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + size = Integer.parseInt(br.readLine()); + arr = new int[size][size]; + + for (int i = 0; i < size; i++){ + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int j = 0; j < size; j++){ + arr[i][j] = Integer.parseInt(st.nextToken()); + } + } + } + + private static int process() { + // 모든 섬들을 서로 다른 번호로 표시 + visited = new boolean[size][size]; + int islandNum = 2; + + for (int i = 0; i < size; i++){ + for (int j = 0; j < size; j++){ + if (arr[i][j] == LAND && !visited[i][j]){ + markIsland(i, j, islandNum); + islandNum++; + } + } + } + + // 각 섬에서 다른 섬으로 가는 최단 거리 찾기 + int minDistance = Integer.MAX_VALUE; + + for (int currentIsland = 2; currentIsland < islandNum; currentIsland++) { + int distance = bfsFromIsland(currentIsland); + minDistance = Math.min(minDistance, distance); + } + + return minDistance; + } + + private static void markIsland(int y, int x, int islandNum) { + Queue q = new LinkedList<>(); + arr[y][x] = islandNum; + q.add(new Point(x, y)); + visited[y][x] = true; + + while (!q.isEmpty()){ + Point p = q.poll(); + for (int i = 0; i < 4; i++) { + int nx = p.x + dx[i]; + int ny = p.y + dy[i]; + if (ny < 0 || ny >= size || nx < 0 || nx >= size) continue; + if (visited[ny][nx] || arr[ny][nx] != LAND) continue; + + visited[ny][nx] = true; + arr[ny][nx] = islandNum; + q.add(new Point(nx, ny)); + } + } + } + + private static int bfsFromIsland(int startIsland) { + Queue queue = new LinkedList<>(); + boolean[][] bfsVisited = new boolean[size][size]; + + // 시작 섬의 모든 셀을 큐에 추가 + for (int i = 0; i < size; i++){ + for (int j = 0; j < size; j++){ + if (arr[i][j] == startIsland){ + queue.add(new Point(j, i)); + bfsVisited[i][j] = true; + } + } + } + + int distance = 0; + + while (!queue.isEmpty()) { + int qSize = queue.size(); + + for (int i = 0; i < qSize; i++) { + Point p = queue.poll(); + + for (int k = 0; k < 4; k++) { + int nx = p.x + dx[k]; + int ny = p.y + dy[k]; + + if (nx < 0 || ny < 0 || nx >= size || ny >= size) continue; + if (bfsVisited[ny][nx]) continue; + + // 다른 섬을 찾았다면 거리 반환 + if (arr[ny][nx] >= 2 && arr[ny][nx] != startIsland) { + return distance; + } + + // 물이면 다음 레벨로 확장 + if (arr[ny][nx] == WATER) { + bfsVisited[ny][nx] = true; + queue.add(new Point(nx, ny)); + } + } + } + distance++; + } + + return Integer.MAX_VALUE; + } +} + +``` \ No newline at end of file