|
| 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 INF = (int) 1e9; |
| 9 | + private static final int[] dx = {1, 0, -1, 0}; |
| 10 | + private static final int[] dy = {0, 1, 0, -1}; |
| 11 | + private static int[][][] dist; |
| 12 | + private static int[][] map; |
| 13 | + private static int N, M; |
| 14 | + public static void main(String[] args) throws IOException { |
| 15 | + init(); |
| 16 | + int answer = BFS(); |
| 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 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 26 | +
|
| 27 | + N = Integer.parseInt(st.nextToken()); |
| 28 | + M = Integer.parseInt(st.nextToken()); |
| 29 | +
|
| 30 | + dist = new int[N+1][M+1][6]; |
| 31 | + map = new int[N+1][M+1]; |
| 32 | +
|
| 33 | + for (int i = 1; i <= N; i++) { |
| 34 | + char[] input = br.readLine().toCharArray(); |
| 35 | + for (int j = 1; j <= M; j++) { |
| 36 | + map[i][j] = input[j-1] - '0'; |
| 37 | + Arrays.fill(dist[i][j], INF); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +
|
| 42 | + private static int BFS() { |
| 43 | + Deque<int[]> q = new ArrayDeque<>(); |
| 44 | + int result = INF; |
| 45 | + dist[1][1][0] = 0; |
| 46 | + // x, y, mode |
| 47 | + q.add(new int[]{1, 1, 0}); |
| 48 | +
|
| 49 | + while (!q.isEmpty()) { |
| 50 | + int[] current = q.poll(); |
| 51 | +
|
| 52 | + if (current[2] == 0) { |
| 53 | + for (int i = 0; i < 4; i++) { |
| 54 | + int nx = current[0] + dx[i]; |
| 55 | + int ny = current[1] + dy[i]; |
| 56 | +
|
| 57 | + if (OOB(nx, ny) || map[nx][ny] == 1 || dist[nx][ny][current[2]] <= dist[current[0]][current[1]][current[2]] + 1) continue; |
| 58 | + dist[nx][ny][current[2]] = dist[current[0]][current[1]][current[2]] + 1; |
| 59 | + q.addLast(new int[]{nx, ny, current[2]}); |
| 60 | + } |
| 61 | +
|
| 62 | + for (int i = 0; i < 4; i++) { |
| 63 | + int newMode = i+2; |
| 64 | + if (dist[current[0]][current[1]][current[2]] < dist[current[0]][current[1]][newMode]) { |
| 65 | + dist[current[0]][current[1]][newMode] = dist[current[0]][current[1]][current[2]]; |
| 66 | + q.addFirst(new int[]{current[0], current[1], newMode}); |
| 67 | + } |
| 68 | + } |
| 69 | + } else if (current[2] == 1) { |
| 70 | + for (int i = 0; i < 4; i++) { |
| 71 | + int nx = current[0] + dx[i]; |
| 72 | + int ny = current[1] + dy[i]; |
| 73 | +
|
| 74 | + if (OOB(nx, ny) || map[nx][ny] == 1 || dist[nx][ny][current[2]] <= dist[current[0]][current[1]][current[2]] + 1) continue; |
| 75 | + dist[nx][ny][current[2]] = dist[current[0]][current[1]][current[2]] + 1; |
| 76 | + q.addLast(new int[]{nx, ny, current[2]}); |
| 77 | + } |
| 78 | + } else { |
| 79 | + if (dist[current[0]][current[1]][current[2]] < dist[current[0]][current[1]][1]) { |
| 80 | + dist[current[0]][current[1]][1] = dist[current[0]][current[1]][current[2]]; |
| 81 | + q.addFirst(new int[]{current[0], current[1], 1}); |
| 82 | + } |
| 83 | +
|
| 84 | + int dir = current[2]-2; |
| 85 | + int nx = current[0] + dx[dir]; |
| 86 | + int ny = current[1] + dy[dir]; |
| 87 | +
|
| 88 | + if (OOB(nx, ny) || dist[nx][ny][current[2]] <= dist[current[0]][current[1]][current[2]] + 1) continue; |
| 89 | + dist[nx][ny][current[2]] = dist[current[0]][current[1]][current[2]] + 1; |
| 90 | + q.addLast(new int[]{nx, ny, current[2]}); |
| 91 | + } |
| 92 | + } |
| 93 | +
|
| 94 | + for (int i = 0; i < 6; i++) { |
| 95 | + result = Math.min(result, dist[N][M][i]); |
| 96 | + } |
| 97 | +
|
| 98 | + return (result == INF) ? -1 : result; |
| 99 | + } |
| 100 | +
|
| 101 | + private static boolean OOB(int nx, int ny) { |
| 102 | + return nx < 1 || nx > N || ny < 1 || ny > M; |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
0 commit comments