Skip to content

Commit 528ce5d

Browse files
authored
Merge pull request #426 from AlgorithmWithGod/lkhyun
[20250709] BOJ / G3 / 벽 부수고 이동하기 / 이강현
2 parents eba88a7 + 3f0c9c8 commit 528ce5d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
public class Main {
5+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
6+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
7+
static StringTokenizer st;
8+
static int N,M,K;
9+
static char[][] matrix;
10+
static int[] di = {-1,1,0,0};
11+
static int[] dj = {0,0,-1,1};
12+
static int min = Integer.MAX_VALUE;
13+
14+
public static void main(String[] args) throws IOException {
15+
st = new StringTokenizer(br.readLine());
16+
N = Integer.parseInt(st.nextToken());
17+
M = Integer.parseInt(st.nextToken());
18+
K = Integer.parseInt(st.nextToken());
19+
20+
matrix = new char[N][M];
21+
for (int i = 0; i < N; i++) {
22+
String line = br.readLine();
23+
for (int j = 0; j < M; j++) {
24+
matrix[i][j] = line.charAt(j);
25+
}
26+
}
27+
28+
BFS();
29+
if(min == Integer.MAX_VALUE){
30+
bw.write("-1");
31+
}else{
32+
bw.write(min+"");
33+
}
34+
bw.close();
35+
}
36+
static void BFS() {
37+
Queue<int[]> q = new LinkedList<>();
38+
boolean[][][] visited = new boolean[N][M][K+1];
39+
q.offer(new int[]{0,0,K,1});//i,j,남은 벽 뚫 개수,현재 거리
40+
visited[0][0][K] = true;
41+
42+
while (!q.isEmpty()) {
43+
int[] cur = q.poll();
44+
if(cur[0] == N-1 && cur[1] == M-1){
45+
min = Math.min(min, cur[3]);
46+
}
47+
48+
for (int k = 0; k < 4; k++) {
49+
int ni = cur[0] + di[k];
50+
int nj = cur[1] + dj[k];
51+
if(ni<0 || ni>=N || nj<0 || nj>=M){
52+
continue;
53+
}
54+
if(matrix[ni][nj]=='0'){
55+
if(!visited[ni][nj][cur[2]]){
56+
q.offer(new int[]{ni,nj,cur[2],cur[3]+1});
57+
visited[ni][nj][cur[2]] = true;
58+
}
59+
}else if(matrix[ni][nj]=='1'){
60+
if(cur[2]>0 && !visited[ni][nj][cur[2]-1]){
61+
q.offer(new int[]{ni,nj,cur[2]-1,cur[3]+1});
62+
visited[ni][nj][cur[2]-1] = true;
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
70+
```

0 commit comments

Comments
 (0)