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