|
| 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][2]; |
| 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 | + PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1[2], o2[2])); |
| 44 | + int result = -1; |
| 45 | + dist[1][1][0] = 0; |
| 46 | + // x, y, dist, skill |
| 47 | + pq.add(new int[]{1, 1, 0, 0}); |
| 48 | +
|
| 49 | + while (!pq.isEmpty()) { |
| 50 | + int[] current = pq.poll(); |
| 51 | +
|
| 52 | + if (dist[current[0]][current[1]][current[3]] < current[2]) continue; |
| 53 | +
|
| 54 | + if (current[0] == N && current[1] == M) { |
| 55 | + result = current[2]; |
| 56 | + break; |
| 57 | + } |
| 58 | +
|
| 59 | + for (int i = 0; i < 4; i++) { |
| 60 | + int nx = current[0] + dx[i]; |
| 61 | + int ny = current[1] + dy[i]; |
| 62 | +
|
| 63 | + if (OOB(nx, ny) || map[nx][ny] == 1 || dist[nx][ny][current[3]] <= current[2]+1) continue; |
| 64 | + dist[nx][ny][current[3]] = current[2]+1; |
| 65 | + pq.add(new int[]{nx, ny, current[2]+1, current[3]}); |
| 66 | + } |
| 67 | +
|
| 68 | + if (current[3] == 0) { |
| 69 | + for (int i = 0; i < 4; i++) { |
| 70 | + switch (i) { |
| 71 | + case 0: |
| 72 | + for (int j = current[0]; j <= N; j++) { |
| 73 | + if (dist[j][current[1]][1] <= current[2]+(j-current[0])) continue; |
| 74 | + dist[j][current[1]][1] = current[2]+(j-current[0]); |
| 75 | + pq.add(new int[]{j, current[1], current[2]+(j-current[0]), 1}); |
| 76 | + } |
| 77 | + break; |
| 78 | + case 1: |
| 79 | + for (int j = current[1]; j <= M; j++) { |
| 80 | + if (dist[current[0]][j][1] <= current[2]+(j-current[1])) continue; |
| 81 | + dist[current[0]][j][1] = current[2]+(j-current[1]); |
| 82 | + pq.add(new int[]{current[0], j, current[2]+(j-current[1]), 1}); |
| 83 | + } |
| 84 | + break; |
| 85 | + case 2: |
| 86 | + for (int j = current[0]; j >= 1; j--) { |
| 87 | + if (dist[j][current[1]][1] <= current[2]+(current[0]-j)) continue; |
| 88 | + dist[j][current[1]][1] = current[2]+(current[0]-j); |
| 89 | + pq.add(new int[]{j, current[1], current[2]+(current[0]-j), 1}); |
| 90 | + } |
| 91 | + break; |
| 92 | + default: |
| 93 | + for (int j = current[1]; j >= 1; j--) { |
| 94 | + if (dist[current[0]][j][1] <= current[2]+(current[1]-j)) continue; |
| 95 | + dist[current[0]][j][1] = current[2]+(current[1]-j); |
| 96 | + pq.add(new int[]{current[0], j, current[2]+(current[1]-j), 1}); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + return result; |
| 104 | + } |
| 105 | +
|
| 106 | + private static boolean OOB(int nx, int ny) { |
| 107 | + return nx < 1 || nx > N || ny < 1 || ny > M; |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
0 commit comments