|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int N; |
| 8 | + static int[][] map; |
| 9 | + static boolean[][] visited; |
| 10 | + static int[] dx = {-1, 1, 0, 0}; |
| 11 | + static int[] dy = {0, 0, -1, 1}; |
| 12 | + static int minDist = Integer.MAX_VALUE; |
| 13 | + |
| 14 | + public static void main(String[] args) throws Exception { |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + N = Integer.parseInt(br.readLine()); |
| 17 | + map = new int[N][N]; |
| 18 | + visited = new boolean[N][N]; |
| 19 | + |
| 20 | + for (int i = 0; i < N; i++) { |
| 21 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 22 | + for (int j = 0; j < N; j++) { |
| 23 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + int islandIdx = 2; |
| 28 | + for (int i = 0; i < N; i++) { |
| 29 | + for (int j = 0; j < N; j++) { |
| 30 | + if (map[i][j] == 1) { |
| 31 | + numbering(i, j, islandIdx++); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + for (int i = 2; i < islandIdx; i++) { |
| 37 | + bfs(i); |
| 38 | + } |
| 39 | + |
| 40 | + System.out.println(minDist); |
| 41 | + } |
| 42 | + |
| 43 | + static void numbering(int r, int c, int idx) { |
| 44 | + Deque<int[]> q = new ArrayDeque<>(); |
| 45 | + q.add(new int[]{r, c}); |
| 46 | + map[r][c] = idx; |
| 47 | + visited[r][c] = true; |
| 48 | + |
| 49 | + while (!q.isEmpty()) { |
| 50 | + int[] cur = q.poll(); |
| 51 | + for (int i = 0; i < 4; i++) { |
| 52 | + int nx = cur[0] + dx[i]; |
| 53 | + int ny = cur[1] + dy[i]; |
| 54 | + if (nx >= 0 && nx < N && ny >= 0 && ny < N && !visited[nx][ny] && map[nx][ny] == 1) { |
| 55 | + visited[nx][ny] = true; |
| 56 | + map[nx][ny] = idx; |
| 57 | + q.add(new int[]{nx, ny}); |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static void bfs(int curIdx) { |
| 64 | + Deque<int[]> q = new ArrayDeque<>(); |
| 65 | + boolean[][] check = new boolean[N][N]; |
| 66 | + |
| 67 | + for (int i = 0; i < N; i++) { |
| 68 | + for (int j = 0; j < N; j++) { |
| 69 | + if (map[i][j] == curIdx) { |
| 70 | + check[i][j] = true; |
| 71 | + q.add(new int[]{i, j, 0}); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + while (!q.isEmpty()) { |
| 77 | + int[] cur = q.poll(); |
| 78 | + int r = cur[0]; |
| 79 | + int c = cur[1]; |
| 80 | + int dist = cur[2]; |
| 81 | + |
| 82 | + if (dist >= minDist) break; |
| 83 | + |
| 84 | + for (int i = 0; i < 4; i++) { |
| 85 | + int nx = r + dx[i]; |
| 86 | + int ny = c + dy[i]; |
| 87 | + |
| 88 | + if (nx >= 0 && nx < N && ny >= 0 && ny < N && !check[nx][ny]) { |
| 89 | + if (map[nx][ny] == 0) { |
| 90 | + check[nx][ny] = true; |
| 91 | + q.add(new int[]{nx, ny, dist + 1}); |
| 92 | + } else if (map[nx][ny] != curIdx) { |
| 93 | + minDist = Math.min(minDist, dist); |
| 94 | + return; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
0 commit comments