|
| 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 = {0, 1, 0, -1, 0}; |
| 9 | + private static final int[] dy = {1, 0, -1, 0, 0}; |
| 10 | + private static Map<String, Integer> ghosts; |
| 11 | + private static boolean[][][] visited; |
| 12 | + private static char[][] map; |
| 13 | + private static int[] start, end; |
| 14 | + private static int N, M; |
| 15 | + public static void main(String[] args) throws IOException { |
| 16 | + init(); |
| 17 | + int answer = BFS(); |
| 18 | +
|
| 19 | + if (answer == -1) bw.write("GG"); |
| 20 | + else bw.write(answer + "\n"); |
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + N = Integer.parseInt(st.nextToken()); |
| 29 | + M = Integer.parseInt(st.nextToken()); |
| 30 | +
|
| 31 | + st = new StringTokenizer(br.readLine()); |
| 32 | + start = new int[2]; |
| 33 | + end = new int[2]; |
| 34 | +
|
| 35 | + start[0] = Integer.parseInt(st.nextToken())-1; |
| 36 | + start[1] = Integer.parseInt(st.nextToken())-1; |
| 37 | + end[0] = Integer.parseInt(st.nextToken())-1; |
| 38 | + end[1] = Integer.parseInt(st.nextToken())-1; |
| 39 | +
|
| 40 | + map = new char[N][M]; |
| 41 | + ghosts = new HashMap<>(); |
| 42 | + visited = new boolean[N][M][4]; |
| 43 | +
|
| 44 | + for (int i = 0; i < N; i++) { |
| 45 | + map[i] = br.readLine().toCharArray(); |
| 46 | + for (int j = 0; j < M; j++) { |
| 47 | + if (map[i][j] != '.' && map[i][j] != '#') { |
| 48 | + ghosts.put(i+","+j, map[i][j] - '0'); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | +
|
| 54 | + private static int BFS() { |
| 55 | + Queue<int[]> q = new ArrayDeque<>(); |
| 56 | + visited[start[0]][start[1]][0] = true; |
| 57 | + q.add(new int[]{start[0], start[1], 0}); |
| 58 | +
|
| 59 | + while (!q.isEmpty()) { |
| 60 | + int[] current = q.poll(); |
| 61 | +
|
| 62 | + if (current[0] == end[0] && current[1] == end[1]) return current[2]; |
| 63 | +
|
| 64 | + for (int i = 0; i < 5; i++) { |
| 65 | + int nx = current[0] + dx[i]; |
| 66 | + int ny = current[1] + dy[i]; |
| 67 | +
|
| 68 | + if (OOB(nx, ny) || map[nx][ny] != '.' || visited[nx][ny][(current[2]+1)%4] || find(nx, ny, current[2]+1)) continue; |
| 69 | + visited[nx][ny][(current[2]+1)%4] = true; |
| 70 | + q.add(new int[]{nx, ny, current[2]+1}); |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + return -1; |
| 75 | + } |
| 76 | +
|
| 77 | + private static boolean find(int nx, int ny, int nTime) { |
| 78 | + for (int i = 0; i < 4; i++) { |
| 79 | + int nnx = nx; |
| 80 | + int nny = ny; |
| 81 | + int d = 0; |
| 82 | + while (!OOB(nnx, nny)) { |
| 83 | + if (map[nnx][nny] == '#') break; |
| 84 | + else if (map[nnx][nny] != '.') { |
| 85 | + int nState = (map[nnx][nny] - '0' + nTime) % 4; |
| 86 | + if (Math.abs(i-nState) == 2) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + break; |
| 90 | + } |
| 91 | + d++; |
| 92 | + nnx = nx + dx[i]*d; |
| 93 | + nny = ny + dy[i]*d; |
| 94 | + } |
| 95 | + } |
| 96 | +
|
| 97 | + return false; |
| 98 | + } |
| 99 | +
|
| 100 | + private static boolean OOB(int nx, int ny) { |
| 101 | + return nx < 0 || nx > N-1 || ny < 0 || ny > M-1; |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments