|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final int[] dx = {1, 0, -1, 0}; |
| 9 | + private static final int[] dy = {0, 1, 0, -1}; |
| 10 | + private static int[][] arr; |
| 11 | + private static boolean[][] visited; |
| 12 | + private static int N, max, min; |
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | +
|
| 16 | + int answer = binarySearch(); |
| 17 | +
|
| 18 | + bw.write(answer + "\n"); |
| 19 | + bw.flush(); |
| 20 | + bw.close(); |
| 21 | + br.close(); |
| 22 | + } |
| 23 | +
|
| 24 | + private static void init() throws IOException { |
| 25 | + N = Integer.parseInt(br.readLine()); |
| 26 | + max = -1; |
| 27 | + min = 201; |
| 28 | +
|
| 29 | + arr = new int[N][N]; |
| 30 | + visited = new boolean[N][N]; |
| 31 | +
|
| 32 | + for (int i = 0; i < N; i++) { |
| 33 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 34 | +
|
| 35 | + for (int j = 0; j < N; j++) { |
| 36 | + arr[i][j] = Integer.parseInt(st.nextToken()); |
| 37 | + max = Math.max(max, arr[i][j]); |
| 38 | + min = Math.min(min, arr[i][j]); |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | +
|
| 43 | + private static int binarySearch() { |
| 44 | + int left = 0; |
| 45 | + int right = 200; |
| 46 | + int result = 200; |
| 47 | +
|
| 48 | + while (left <= right) { |
| 49 | + int mid = left + (right - left) / 2; |
| 50 | +
|
| 51 | + if (valid(mid)) { |
| 52 | + result = mid; |
| 53 | + right = mid-1; |
| 54 | + } else { |
| 55 | + left = mid+1; |
| 56 | + } |
| 57 | + } |
| 58 | +
|
| 59 | + return result; |
| 60 | + } |
| 61 | +
|
| 62 | + private static boolean valid(int diff) { |
| 63 | + for (int i = min; i <= max; i++) { |
| 64 | + int temp = i + diff; |
| 65 | +
|
| 66 | + if (arr[0][0] < i || arr[0][0] > temp || arr[N-1][N-1] < i || arr[N-1][N-1] > temp) continue; |
| 67 | +
|
| 68 | + if (canReach(i, temp)) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + return false; |
| 74 | + } |
| 75 | +
|
| 76 | + private static boolean canReach(int min, int max) { |
| 77 | + Queue<int[]> q = new ArrayDeque<>(); |
| 78 | + for (int i = 0; i < N; i++) { |
| 79 | + Arrays.fill(visited[i], false); |
| 80 | + } |
| 81 | + visited[0][0] = true; |
| 82 | + q.add(new int[]{0, 0}); |
| 83 | +
|
| 84 | + while (!q.isEmpty()) { |
| 85 | + int[] current = q.poll(); |
| 86 | +
|
| 87 | + if (current[0] == N-1 && current[1] == N-1) { |
| 88 | + return true; |
| 89 | + } |
| 90 | +
|
| 91 | + for (int i = 0; i < 4; i++) { |
| 92 | + int nx = current[0] + dx[i]; |
| 93 | + int ny = current[1] + dy[i]; |
| 94 | +
|
| 95 | + if (OOB(nx, ny) || arr[nx][ny] > max || arr[nx][ny] < min || visited[nx][ny]) continue; |
| 96 | + visited[nx][ny] = true; |
| 97 | + q.add(new int[] {nx, ny}); |
| 98 | + } |
| 99 | + } |
| 100 | +
|
| 101 | + return false; |
| 102 | + } |
| 103 | +
|
| 104 | + private static boolean OOB(int nx, int ny) { |
| 105 | + return nx < 0 || nx > N-1 || ny < 0 || ny > N-1; |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
0 commit comments