From e6af7560708e9552e8c37a4c6dca5606985c00e9 Mon Sep 17 00:00:00 2001 From: Jinyeong Seol Date: Fri, 11 Jul 2025 21:24:36 +0900 Subject: [PATCH] =?UTF-8?q?[20250711]=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=20/=20=EC=84=A4=EC=A7=84=EC=98=81?= 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" | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 "Seol-JY/202507/11 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/Seol-JY/202507/11 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/Seol-JY/202507/11 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..bdb7633b --- /dev/null +++ "b/Seol-JY/202507/11 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,75 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N, M, K; + static int[][] map; + static boolean[][][] visited; + static int[] dx = {-1, 1, 0, 0}; + static int[] dy = {0, 0, -1, 1}; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + map = new int[N][M]; + visited = new boolean[N][M][K + 1]; + + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + map[i][j] = line.charAt(j) - '0'; + } + } + + System.out.println(bfs()); + } + + static int bfs() { + ArrayDeque queue = new ArrayDeque<>(); + queue.offer(new Node(0, 0, 0, 1)); + visited[0][0][0] = true; + + while (!queue.isEmpty()) { + Node current = queue.poll(); + + if (current.x == N - 1 && current.y == M - 1) { + return current.dist; + } + + for (int i = 0; i < 4; i++) { + int nx = current.x + dx[i]; + int ny = current.y + dy[i]; + + if (nx < 0 || nx >= N || ny < 0 || ny >= M) continue; + + if (map[nx][ny] == 0 && !visited[nx][ny][current.broken]) { + visited[nx][ny][current.broken] = true; + queue.offer(new Node(nx, ny, current.broken, current.dist + 1)); + } else if (map[nx][ny] == 1 && current.broken < K && !visited[nx][ny][current.broken + 1]) { + visited[nx][ny][current.broken + 1] = true; + queue.offer(new Node(nx, ny, current.broken + 1, current.dist + 1)); + } + } + } + + return -1; + } + + static class Node { + int x, y, broken, dist; + + Node(int x, int y, int broken, int dist) { + this.x = x; + this.y = y; + this.broken = broken; + this.dist = dist; + } + } +} +```