|
| 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, -1, -1, 0, 0, 1, 1, 1}; |
| 9 | + private static final int[] dy = {-1, 0, 1, -1, 1, -1, 0, 1}; |
| 10 | + private static List<Integer> list; |
| 11 | + private static char[][] map; |
| 12 | + private static boolean[][] visited; |
| 13 | + private static int[][] height; |
| 14 | + private static int[] start; |
| 15 | + private static int N, K; |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + init(); |
| 18 | + int answer = twoPointer(); |
| 19 | +
|
| 20 | + bw.write(answer + "\n"); |
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + N = Integer.parseInt(br.readLine()); |
| 28 | +
|
| 29 | + map = new char[N][N]; |
| 30 | + height = new int[N][N]; |
| 31 | + visited = new boolean[N][N]; |
| 32 | + start = new int[2]; |
| 33 | +
|
| 34 | + for (int i = 0; i < N; i++) { |
| 35 | + map[i] = br.readLine().toCharArray(); |
| 36 | + for (int j = 0; j < N; j++) { |
| 37 | + if (map[i][j] == 'P') { |
| 38 | + start[0] = i; |
| 39 | + start[1] = j; |
| 40 | + } |
| 41 | + if (map[i][j] == 'K') K++; |
| 42 | + } |
| 43 | + } |
| 44 | +
|
| 45 | + Set<Integer> set = new TreeSet<>(); |
| 46 | + for (int i = 0; i < N; i++) { |
| 47 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 48 | + for (int j = 0; j < N; j++) { |
| 49 | + height[i][j] = Integer.parseInt(st.nextToken()); |
| 50 | + set.add(height[i][j]); |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + list = new ArrayList<>(set); |
| 55 | + } |
| 56 | +
|
| 57 | + private static int twoPointer() { |
| 58 | + int result = Integer.MAX_VALUE; |
| 59 | + int left = 0; |
| 60 | +
|
| 61 | + for (int right = 0; right < list.size(); right++) { |
| 62 | + while (left <= right) { |
| 63 | + if (valid(list.get(left), list.get(right))) { |
| 64 | + result = Math.min(result, list.get(right) - list.get(left)); |
| 65 | + left++; |
| 66 | + } else break; |
| 67 | + } |
| 68 | + } |
| 69 | +
|
| 70 | + return result; |
| 71 | + } |
| 72 | +
|
| 73 | + private static boolean valid(int min, int max) { |
| 74 | + if (height[start[0]][start[1]] < min || height[start[0]][start[1]] > max) return false; |
| 75 | +
|
| 76 | + Queue<int[]> q = new ArrayDeque<>(); |
| 77 | + for (int i = 0; i < N; i++) Arrays.fill(visited[i], false); |
| 78 | + visited[start[0]][start[1]] = true; |
| 79 | + q.add(new int[]{start[0], start[1]}); |
| 80 | +
|
| 81 | + int temp = 0; |
| 82 | +
|
| 83 | + while (!q.isEmpty()) { |
| 84 | + int[] current = q.poll(); |
| 85 | +
|
| 86 | + if (map[current[0]][current[1]] == 'K') temp++; |
| 87 | +
|
| 88 | + for (int i = 0; i < 8; i++) { |
| 89 | + int nx = current[0] + dx[i]; |
| 90 | + int ny = current[1] + dy[i]; |
| 91 | +
|
| 92 | + if (OOB(nx, ny) || visited[nx][ny] || height[nx][ny] < min || height[nx][ny] > max) continue; |
| 93 | +
|
| 94 | + visited[nx][ny] = true; |
| 95 | + q.add(new int[]{nx, ny}); |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + return temp == K; |
| 100 | + } |
| 101 | +
|
| 102 | + private static boolean OOB(int nx, int ny) { |
| 103 | + return nx < 0 || nx > N-1 || ny < 0 || ny > N-1; |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
0 commit comments