Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Seol-JY/202507/11 BOJ G3 벽 부수고 이동하기 2.md
Original file line number Diff line number Diff line change
@@ -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<Node> 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;
}
}
}
```