From 691c2b664e363e9abd8a2221dda6a969a28e77e7 Mon Sep 17 00:00:00 2001 From: zinnnn37 Date: Sun, 14 Dec 2025 21:45:48 +0900 Subject: [PATCH] =?UTF-8?q?[20251214]=20BOJ=20/=20G3=20/=20=EB=B2=BD=20?= =?UTF-8?q?=EB=B6=80=EC=88=98=EA=B3=A0=20=EC=9D=B4=EB=8F=99=ED=95=98?= =?UTF-8?q?=EA=B8=B0=202=20/=20=EA=B9=80=EB=AF=BC=EC=A7=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\217\231\355\225\230\352\270\260 2.md" | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 "zinnnn37/202512/14 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" diff --git "a/zinnnn37/202512/14 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" "b/zinnnn37/202512/14 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" new file mode 100644 index 00000000..e65c2f6e --- /dev/null +++ "b/zinnnn37/202512/14 BOJ G3 \353\262\275 \353\266\200\354\210\230\352\263\240 \354\235\264\353\217\231\355\225\230\352\270\260 2.md" @@ -0,0 +1,100 @@ +```java +import java.io.*; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + +public class BJ_14442_벽_부수고_이동하기_2 { + + private static final int[] dx = { 0, 1, 0, -1 }; + private static final int[] dy = { 1, 0, -1, 0 }; + + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static StringTokenizer st; + + private static int N, M, K; + private static char[][] matrix; + private static boolean[][][] visited; + private static Queue q; + + private static class Node { + int x; + int y; + int k; + int cnt; + + public Node(int x, int y, int k, int cnt) { + this.x = x; + this.y = y; + this.k = k; + this.cnt = cnt; + } + + } + + public static void main(String[] args) throws IOException { + init(); + sol(); + + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + matrix = new char[N][M]; + for (int i = 0; i < N; i++) { + String input = br.readLine(); + for (int j = 0; j < M; j++) { + matrix[i][j] = input.charAt(j); + } + } + + visited = new boolean[N][M][K + 1]; + q = new ArrayDeque<>(); + } + + private static void sol() throws IOException { + q.offer(new Node(0, 0, 0, 1)); + visited[0][0][0] = true; + + while (!q.isEmpty()) { + Node cur = q.poll(); + + if (cur.x == N - 1 && cur.y == M - 1) { + bw.write(cur.cnt + ""); + return; + } + + for (int d = 0; d < 4; d++) { + int nx = cur.x + dx[d]; + int ny = cur.y + dy[d]; + + if (OOB(nx, ny)) { + continue; + } + + if (!visited[nx][ny][cur.k] && matrix[nx][ny] == '0') { + visited[nx][ny][cur.k] = true; + q.offer(new Node(nx, ny, cur.k, cur.cnt + 1)); + } else if (cur.k < K && !visited[nx][ny][cur.k + 1] && matrix[nx][ny] == '1') { + visited[nx][ny][cur.k + 1] = true; + q.offer(new Node(nx, ny, cur.k + 1, cur.cnt + 1)); + } + } + } + bw.write("-1"); + } + + private static boolean OOB(int x, int y) { + return x < 0 || N <= x || y < 0 || M <= y; + } + +} +``` \ No newline at end of file