|
| 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 h, w; |
| 11 | + private static char[][] map; |
| 12 | + private static int[][][] doors; |
| 13 | + private static int[][] prisoners; |
| 14 | +
|
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + int T = Integer.parseInt(br.readLine()); |
| 17 | +
|
| 18 | + while (T-->0) { |
| 19 | + int answer = 10001; |
| 20 | +
|
| 21 | + init(); |
| 22 | + for (int i = 0; i < 3; i++) { |
| 23 | + BFS(i); |
| 24 | + } |
| 25 | +
|
| 26 | + for (int i = 1; i <= h; i++) { |
| 27 | + for (int j = 1; j <= w; j++) { |
| 28 | + if (map[i][j] == '.' || map[i][j] == '$') { |
| 29 | + int sum = doors[0][i][j] + doors[1][i][j] + doors[2][i][j]; |
| 30 | + answer = Math.min(answer, sum); |
| 31 | + } else if (map[i][j] == '#') { |
| 32 | + int sum = doors[0][i][j] + doors[1][i][j] + doors[2][i][j] - 2; |
| 33 | + answer = Math.min(answer, sum); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + bw.write(answer + "\n"); |
| 39 | + } |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + br.close(); |
| 43 | + } |
| 44 | +
|
| 45 | + private static void init() throws IOException { |
| 46 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 47 | + h = Integer.parseInt(st.nextToken()); |
| 48 | + w = Integer.parseInt(st.nextToken()); |
| 49 | +
|
| 50 | + map = new char[h+2][w+2]; |
| 51 | + doors = new int[3][h+2][w+2]; |
| 52 | + prisoners = new int[2][2]; |
| 53 | + int index = 0; |
| 54 | +
|
| 55 | + for (int i = 1; i <= h; i++) { |
| 56 | + char[] input = br.readLine().toCharArray(); |
| 57 | +
|
| 58 | + for (int j = 1; j <= w; j++) { |
| 59 | + map[i][j] = input[j-1]; |
| 60 | + if (map[i][j] == '$') { |
| 61 | + prisoners[index][0] = i; |
| 62 | + prisoners[index++][1] = j; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +
|
| 67 | + for (int i = 0; i <= h+1; i++) { |
| 68 | + map[i][0] = '.'; |
| 69 | + map[i][w+1] = '.'; |
| 70 | + } |
| 71 | +
|
| 72 | + for (int i = 0; i <= w+1; i++) { |
| 73 | + map[0][i] = '.'; |
| 74 | + map[h+1][i] = '.'; |
| 75 | + } |
| 76 | + } |
| 77 | +
|
| 78 | + private static void BFS(int index) { |
| 79 | + Queue<int[]> q = new ArrayDeque<>(); |
| 80 | +
|
| 81 | + for (int i = 0; i <= h+1; i++) { |
| 82 | + Arrays.fill(doors[index][i], 10001); |
| 83 | + } |
| 84 | +
|
| 85 | + if (index == 2) { |
| 86 | + doors[index][0][0] = 0; |
| 87 | + q.add (new int[] {0, 0}); |
| 88 | + } |
| 89 | + else { |
| 90 | + doors[index][prisoners[index][0]][prisoners[index][1]] = 0; |
| 91 | + q.add(new int[] {prisoners[index][0], prisoners[index][1]}); |
| 92 | + } |
| 93 | +
|
| 94 | + while (!q.isEmpty()) { |
| 95 | + int[] current = q.poll(); |
| 96 | +
|
| 97 | + for (int i = 0; i < 4; i++) { |
| 98 | + int nx = current[0] + dx[i]; |
| 99 | + int ny = current[1] + dy[i]; |
| 100 | +
|
| 101 | + if (OOB(nx, ny) || map[nx][ny] == '*') continue; |
| 102 | + if (map[nx][ny] == '#' && doors[index][current[0]][current[1]]+1 < doors[index][nx][ny]) { |
| 103 | + doors[index][nx][ny] = doors[index][current[0]][current[1]]+1; |
| 104 | + q.add(new int[] {nx, ny}); |
| 105 | + } else if ((map[nx][ny] == '.' || map[nx][ny] == '$') && doors[index][current[0]][current[1]] < doors[index][nx][ny]) { |
| 106 | + doors[index][nx][ny] = doors[index][current[0]][current[1]]; |
| 107 | + q.add(new int[] {nx, ny}); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +
|
| 113 | + private static boolean OOB(int nx, int ny) { |
| 114 | + return nx < 0 || nx > h+1 || ny < 0 || ny > w+1; |
| 115 | + } |
| 116 | +} |
| 117 | +``` |
0 commit comments