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; + } + } +} +```