|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.ArrayDeque; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Deque; |
| 6 | +import java.util.StringTokenizer; |
| 7 | + |
| 8 | +public class BJ_14497_주난의_난 { |
| 9 | + |
| 10 | + private static final int INF = 987654321; |
| 11 | + private static final int[] dx = { -1, 1, 0, 0 }; |
| 12 | + private static final int[] dy = { 0, 0, -1, 1 }; |
| 13 | + |
| 14 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 15 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 16 | + private static StringTokenizer st; |
| 17 | + |
| 18 | + private static int N, M; |
| 19 | + private static int[] points; |
| 20 | + private static int[][] dist; |
| 21 | + private static char[][] matrix; |
| 22 | + private static Deque<Node> q; |
| 23 | + |
| 24 | + private static class Node { |
| 25 | + int x; |
| 26 | + int y; |
| 27 | + int cnt; |
| 28 | + |
| 29 | + public Node(int x, int y, int cnt) { |
| 30 | + this.x = x; |
| 31 | + this.y = y; |
| 32 | + this.cnt = cnt; |
| 33 | + } |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + public static void main(String[] args) throws IOException { |
| 38 | + init(); |
| 39 | + sol(); |
| 40 | + |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + br.close(); |
| 44 | + } |
| 45 | + |
| 46 | + private static void init() throws IOException { |
| 47 | + st = new StringTokenizer(br.readLine()); |
| 48 | + N = Integer.parseInt(st.nextToken()); |
| 49 | + M = Integer.parseInt(st.nextToken()); |
| 50 | + |
| 51 | + points = new int[4]; |
| 52 | + st = new StringTokenizer(br.readLine()); |
| 53 | + for (int i = 0; i < 4; i++) { |
| 54 | + points[i] = Integer.parseInt(st.nextToken()) - 1; |
| 55 | + } |
| 56 | + |
| 57 | + dist = new int[N][M]; |
| 58 | + for (int i = 0; i < N; i++) { |
| 59 | + Arrays.fill(dist[i], INF); |
| 60 | + } |
| 61 | + |
| 62 | + matrix = new char[N][M]; |
| 63 | + for (int i = 0; i < N; i++) { |
| 64 | + String line = br.readLine(); |
| 65 | + for (int j = 0; j < M; j++) { |
| 66 | + matrix[i][j] = line.charAt(j); |
| 67 | + } |
| 68 | + } |
| 69 | + q = new ArrayDeque<>(); |
| 70 | + } |
| 71 | + |
| 72 | + private static void sol() throws IOException { |
| 73 | + q.offer(new Node(points[0], points[1], 0)); |
| 74 | + dist[points[0]][points[1]] = 0; |
| 75 | + |
| 76 | + while (!q.isEmpty()) { |
| 77 | + Node cur = q.poll(); |
| 78 | + |
| 79 | + if (dist[cur.x][cur.y] < cur.cnt) continue; |
| 80 | + |
| 81 | + if (matrix[cur.x][cur.y] == '#') { |
| 82 | + bw.write(cur.cnt + ""); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + for (int d = 0; d < 4; d++) { |
| 87 | + int nx = cur.x + dx[d]; |
| 88 | + int ny = cur.y + dy[d]; |
| 89 | + |
| 90 | + if (OOB(nx, ny)) continue; |
| 91 | + |
| 92 | + int newDist = cur.cnt + (matrix[nx][ny] == '0' ? 0 : 1); |
| 93 | + |
| 94 | + if (newDist < dist[nx][ny]) { |
| 95 | + dist[nx][ny] = newDist; |
| 96 | + |
| 97 | + if (matrix[nx][ny] == '0') { |
| 98 | + q.offerFirst(new Node(nx, ny, newDist)); |
| 99 | + } else { |
| 100 | + q.offerLast(new Node(nx, ny, newDist)); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static boolean OOB(int x, int y) { |
| 108 | + return x < 0 || N <= x || y < 0 || M <= y; |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | +``` |
0 commit comments