Skip to content

Commit bea47bb

Browse files
authored
[20260124] BOJ / P4 / 탈옥 / 김민진
1 parent ead06eb commit bea47bb

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
```java
2+
import java.io.*;
3+
import java.util.ArrayDeque;
4+
import java.util.Arrays;
5+
import java.util.Deque;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ_9376_탈옥 {
9+
10+
private static final int INF = 10001;
11+
private static final int[] dx = { -1, 1, 0, 0 };
12+
private static final int[] dy = { 0, 0, -1, 1 };
13+
14+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
16+
private static final StringBuilder sb = new StringBuilder();
17+
private static StringTokenizer st;
18+
19+
private static int H, W;
20+
private static int[][] prisoners;
21+
private static int[][][] dist;
22+
private static char[][] matrix;
23+
private static Deque<Node> dq;
24+
25+
private static class Node {
26+
int x;
27+
int y;
28+
int cnt;
29+
30+
Node(int x, int y, int cnt) {
31+
this.x = x;
32+
this.y = y;
33+
this.cnt = cnt;
34+
}
35+
36+
}
37+
38+
public static void main(String[] args) throws IOException {
39+
int T = Integer.parseInt(br.readLine());
40+
41+
while (T-- > 0) {
42+
init();
43+
sol();
44+
}
45+
46+
bw.write(sb.toString());
47+
bw.flush();
48+
bw.close();
49+
br.close();
50+
}
51+
52+
private static void init() throws IOException {
53+
st = new StringTokenizer(br.readLine());
54+
H = Integer.parseInt(st.nextToken());
55+
W = Integer.parseInt(st.nextToken());
56+
57+
prisoners = new int[2][2];
58+
int idx = 0;
59+
60+
matrix = new char[H + 2][W + 2];
61+
dist = new int[3][H + 2][W + 2];
62+
63+
for (int i = 0; i < H + 2; i++) {
64+
Arrays.fill(dist[0][i], INF);
65+
Arrays.fill(dist[1][i], INF);
66+
Arrays.fill(dist[2][i], INF);
67+
Arrays.fill(matrix[i], '.');
68+
}
69+
70+
for (int i = 1; i <= H; i++) {
71+
String line = br.readLine();
72+
for (int j = 1; j <= W; j++) {
73+
matrix[i][j] = line.charAt(j - 1);
74+
75+
if (matrix[i][j] == '$') {
76+
prisoners[idx][0] = i;
77+
prisoners[idx++][1] = j;
78+
matrix[i][j] = '.';
79+
}
80+
}
81+
}
82+
83+
dq = new ArrayDeque<>();
84+
}
85+
86+
private static void sol() {
87+
bfs(0, 0, 0);
88+
bfs(1, prisoners[0][0], prisoners[0][1]);
89+
bfs(2, prisoners[1][0], prisoners[1][1]);
90+
91+
int ans = INF;
92+
for (int i = 0; i < H + 2; i++) {
93+
for (int j = 0; j < W + 2; j++) {
94+
if (matrix[i][j] == '*') continue;
95+
96+
int sum = dist[0][i][j] + dist[1][i][j] + dist[2][i][j];
97+
if (matrix[i][j] == '#') sum -= 2;
98+
99+
ans = Math.min(ans, sum);
100+
}
101+
}
102+
103+
sb.append(ans).append('\n');
104+
}
105+
106+
private static void bfs(int id, int x, int y) {
107+
dq.clear();
108+
dq.offer(new Node(x, y, 0));
109+
dist[id][x][y] = 0;
110+
111+
while (!dq.isEmpty()) {
112+
Node cur = dq.poll();
113+
114+
if (dist[id][cur.x][cur.y] < cur.cnt) continue;
115+
116+
for (int d = 0; d < 4; d++) {
117+
int nx = cur.x + dx[d];
118+
int ny = cur.y + dy[d];
119+
120+
if (OOB(nx, ny) || matrix[nx][ny] == '*') continue;
121+
122+
int newDist = matrix[nx][ny] == '#' ? cur.cnt + 1 : cur.cnt;
123+
124+
if (newDist < dist[id][nx][ny]) {
125+
dist[id][nx][ny] = newDist;
126+
127+
if (newDist == cur.cnt) {
128+
dq.offerFirst(new Node(nx, ny, newDist));
129+
} else {
130+
dq.offerLast(new Node(nx, ny, newDist));
131+
}
132+
}
133+
}
134+
}
135+
}
136+
137+
private static boolean OOB(int x, int y) {
138+
return x < 0 || H + 2 <= x || y < 0 || W + 2 <= y;
139+
}
140+
141+
}
142+
```

0 commit comments

Comments
 (0)