|
| 1 | +```java |
| 2 | +import java.awt.*; |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.util.*; |
| 7 | + |
| 8 | +public class Main { |
| 9 | + static int[][] arr; |
| 10 | + static boolean[][] visited; |
| 11 | + static int[] dy = {-1,0,1,0}; |
| 12 | + static int[] dx = {0,1,0,-1}; |
| 13 | + static int size; |
| 14 | + static final int LAND = 1; |
| 15 | + static final int WATER = 0; |
| 16 | + |
| 17 | + public static void main(String[] args) throws IOException { |
| 18 | + init(); |
| 19 | + int result = process(); |
| 20 | + System.out.println(result); |
| 21 | + } |
| 22 | + |
| 23 | + private static void init() throws IOException { |
| 24 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 25 | + size = Integer.parseInt(br.readLine()); |
| 26 | + arr = new int[size][size]; |
| 27 | + |
| 28 | + for (int i = 0; i < size; i++){ |
| 29 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 30 | + for (int j = 0; j < size; j++){ |
| 31 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private static int process() { |
| 37 | + // 모든 섬들을 서로 다른 번호로 표시 |
| 38 | + visited = new boolean[size][size]; |
| 39 | + int islandNum = 2; |
| 40 | + |
| 41 | + for (int i = 0; i < size; i++){ |
| 42 | + for (int j = 0; j < size; j++){ |
| 43 | + if (arr[i][j] == LAND && !visited[i][j]){ |
| 44 | + markIsland(i, j, islandNum); |
| 45 | + islandNum++; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // 각 섬에서 다른 섬으로 가는 최단 거리 찾기 |
| 51 | + int minDistance = Integer.MAX_VALUE; |
| 52 | + |
| 53 | + for (int currentIsland = 2; currentIsland < islandNum; currentIsland++) { |
| 54 | + int distance = bfsFromIsland(currentIsland); |
| 55 | + minDistance = Math.min(minDistance, distance); |
| 56 | + } |
| 57 | + |
| 58 | + return minDistance; |
| 59 | + } |
| 60 | + |
| 61 | + private static void markIsland(int y, int x, int islandNum) { |
| 62 | + Queue<Point> q = new LinkedList<>(); |
| 63 | + arr[y][x] = islandNum; |
| 64 | + q.add(new Point(x, y)); |
| 65 | + visited[y][x] = true; |
| 66 | + |
| 67 | + while (!q.isEmpty()){ |
| 68 | + Point p = q.poll(); |
| 69 | + for (int i = 0; i < 4; i++) { |
| 70 | + int nx = p.x + dx[i]; |
| 71 | + int ny = p.y + dy[i]; |
| 72 | + if (ny < 0 || ny >= size || nx < 0 || nx >= size) continue; |
| 73 | + if (visited[ny][nx] || arr[ny][nx] != LAND) continue; |
| 74 | + |
| 75 | + visited[ny][nx] = true; |
| 76 | + arr[ny][nx] = islandNum; |
| 77 | + q.add(new Point(nx, ny)); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static int bfsFromIsland(int startIsland) { |
| 83 | + Queue<Point> queue = new LinkedList<>(); |
| 84 | + boolean[][] bfsVisited = new boolean[size][size]; |
| 85 | + |
| 86 | + // 시작 섬의 모든 셀을 큐에 추가 |
| 87 | + for (int i = 0; i < size; i++){ |
| 88 | + for (int j = 0; j < size; j++){ |
| 89 | + if (arr[i][j] == startIsland){ |
| 90 | + queue.add(new Point(j, i)); |
| 91 | + bfsVisited[i][j] = true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + int distance = 0; |
| 97 | + |
| 98 | + while (!queue.isEmpty()) { |
| 99 | + int qSize = queue.size(); |
| 100 | + |
| 101 | + for (int i = 0; i < qSize; i++) { |
| 102 | + Point p = queue.poll(); |
| 103 | + |
| 104 | + for (int k = 0; k < 4; k++) { |
| 105 | + int nx = p.x + dx[k]; |
| 106 | + int ny = p.y + dy[k]; |
| 107 | + |
| 108 | + if (nx < 0 || ny < 0 || nx >= size || ny >= size) continue; |
| 109 | + if (bfsVisited[ny][nx]) continue; |
| 110 | + |
| 111 | + // 다른 섬을 찾았다면 거리 반환 |
| 112 | + if (arr[ny][nx] >= 2 && arr[ny][nx] != startIsland) { |
| 113 | + return distance; |
| 114 | + } |
| 115 | + |
| 116 | + // 물이면 다음 레벨로 확장 |
| 117 | + if (arr[ny][nx] == WATER) { |
| 118 | + bfsVisited[ny][nx] = true; |
| 119 | + queue.add(new Point(nx, ny)); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + distance++; |
| 124 | + } |
| 125 | + |
| 126 | + return Integer.MAX_VALUE; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +``` |
0 commit comments