Skip to content

Commit dcde9c5

Browse files
committed
[20251216] BOJ / G2 / 벽 부수고 이동하기 3 / 김민진
1 parent 689cd3a commit dcde9c5

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
```java
2+
import java.io.*;
3+
import java.util.ArrayDeque;
4+
import java.util.Arrays;
5+
import java.util.Queue;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ_16933_벽_부수고_이동하기_3 {
9+
10+
private static final int[] dx = { 0, -1, 0, 1 };
11+
private static final int[] dy = { -1, 0, 1, 0 };
12+
13+
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
15+
private static StringTokenizer st;
16+
17+
private static int N, M, K;
18+
private static char[][] matrix;
19+
private static int[][] visited;
20+
private static Queue<Node> q;
21+
22+
private static class Node {
23+
int x;
24+
int y;
25+
int k;
26+
int cnt;
27+
boolean isDay;
28+
29+
public Node(int x, int y, int k, int cnt, boolean isDay) {
30+
this.x = x;
31+
this.y = y;
32+
this.k = k;
33+
this.cnt = cnt;
34+
this.isDay = isDay;
35+
}
36+
37+
}
38+
39+
public static void main(String[] args) throws IOException {
40+
init();
41+
sol();
42+
43+
bw.flush();
44+
bw.close();
45+
br.close();
46+
}
47+
48+
private static void init() throws IOException {
49+
st = new StringTokenizer(br.readLine());
50+
N = Integer.parseInt(st.nextToken());
51+
M = Integer.parseInt(st.nextToken());
52+
K = Integer.parseInt(st.nextToken());
53+
54+
matrix = new char[N][M];
55+
for (int i = 0; i < N; i++) {
56+
matrix[i] = br.readLine().toCharArray();
57+
}
58+
59+
visited = new int[N][M];
60+
for (int i = 0; i < N; i++) {
61+
Arrays.fill(visited[i], Integer.MAX_VALUE);
62+
}
63+
64+
q = new ArrayDeque<>();
65+
}
66+
67+
private static void sol() throws IOException {
68+
q.offer(new Node(0, 0, 0, 1, true));
69+
visited[0][0] = 0;
70+
71+
while (!q.isEmpty()) {
72+
Node cur = q.poll();
73+
74+
if (cur.x == N - 1 && cur.y == M - 1) {
75+
bw.write(cur.cnt + "");
76+
return;
77+
}
78+
79+
for (int d = 0; d < 4; d++) {
80+
int nx = cur.x + dx[d];
81+
int ny = cur.y + dy[d];
82+
83+
if (OOB(nx, ny) || visited[nx][ny] <= cur.k) continue;
84+
85+
if (matrix[nx][ny] == '0') {
86+
visited[nx][ny] = cur.k;
87+
q.offer(new Node(nx, ny, cur.k, cur.cnt + 1, !cur.isDay));
88+
} else if (cur.k < K && visited[nx][ny] > cur.k + 1) {
89+
if (cur.isDay) {
90+
visited[nx][ny] = cur.k + 1;
91+
q.offer(new Node(nx, ny, cur.k + 1, cur.cnt + 1, false));
92+
} else {
93+
q.offer(new Node(cur.x, cur.y, cur.k, cur.cnt + 1, true));
94+
}
95+
}
96+
}
97+
}
98+
bw.write("-1");
99+
}
100+
101+
private static boolean OOB(int x, int y) {
102+
return x < 0 || N <= x || y < 0 || M <= y;
103+
}
104+
105+
}
106+
```

0 commit comments

Comments
 (0)