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