Skip to content

Commit 90bf86b

Browse files
authored
Merge pull request #736 from AlgorithmWithGod/JHLEE325
[20250825] BOJ / G4 / 불 / 이준희
2 parents 2c1b8a5 + 06fdfb2 commit 90bf86b

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

JHLEE325/202508/25 BOJ G4 불.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int[][] map;
8+
static int w, h;
9+
static Queue<int[]> fireQ;
10+
static Queue<int[]> humanQ;
11+
static StringBuilder sb = new StringBuilder();
12+
static int[] dr = {-1, 1, 0, 0};
13+
static int[] dc = {0, 0, -1, 1};
14+
static boolean[][] fireVisited;
15+
static boolean[][] humanVisited;
16+
17+
public static void main(String[] args) throws Exception {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
StringTokenizer st;
20+
21+
int T = Integer.parseInt(br.readLine());
22+
23+
for (int t = 0; t < T; t++) {
24+
st = new StringTokenizer(br.readLine());
25+
w = Integer.parseInt(st.nextToken());
26+
h = Integer.parseInt(st.nextToken());
27+
28+
map = new int[h][w];
29+
fireVisited = new boolean[h][w];
30+
humanVisited = new boolean[h][w];
31+
32+
fireQ = new ArrayDeque<>();
33+
humanQ = new ArrayDeque<>();
34+
35+
int sr = -1, sc = -1;
36+
37+
for (int i = 0; i < h; i++) {
38+
String str = br.readLine();
39+
for (int j = 0; j < w; j++) {
40+
char ch = str.charAt(j);
41+
if (ch == '#') {
42+
map[i][j] = 1;
43+
} else {
44+
map[i][j] = 0;
45+
if (ch == '@') {
46+
humanQ.add(new int[]{i, j});
47+
humanVisited[i][j] = true;
48+
sr = i; sc = j;
49+
} else if (ch == '*') {
50+
fireQ.add(new int[]{i, j});
51+
fireVisited[i][j] = true;
52+
}
53+
}
54+
}
55+
}
56+
57+
if (sr == 0 || sr == h - 1 || sc == 0 || sc == w - 1) {
58+
sb.append("1\n");
59+
} else {
60+
bfs();
61+
}
62+
}
63+
System.out.print(sb.toString());
64+
}
65+
66+
public static void bfs() {
67+
int time = 1;
68+
69+
while (!humanQ.isEmpty()) {
70+
int fs = fireQ.size();
71+
for (int i = 0; i < fs; i++) {
72+
int[] cur = fireQ.poll();
73+
for (int d = 0; d < 4; d++) {
74+
int nr = cur[0] + dr[d];
75+
int nc = cur[1] + dc[d];
76+
if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
77+
if (map[nr][nc] == 1 || fireVisited[nr][nc]) continue;
78+
fireVisited[nr][nc] = true;
79+
fireQ.add(new int[]{nr, nc});
80+
}
81+
}
82+
83+
int hs = humanQ.size();
84+
for (int i = 0; i < hs; i++) {
85+
int[] cur = humanQ.poll();
86+
87+
if (cur[0] == 0 || cur[0] == h - 1 || cur[1] == 0 || cur[1] == w - 1) {
88+
sb.append(time).append('\n');
89+
humanQ.clear();
90+
fireQ.clear();
91+
return;
92+
}
93+
94+
for (int d = 0; d < 4; d++) {
95+
int nr = cur[0] + dr[d];
96+
int nc = cur[1] + dc[d];
97+
if (nr < 0 || nr >= h || nc < 0 || nc >= w) continue;
98+
if (map[nr][nc] == 1) continue;
99+
if (humanVisited[nr][nc]) continue;
100+
if (fireVisited[nr][nc]) continue;
101+
humanVisited[nr][nc] = true;
102+
humanQ.add(new int[]{nr, nc});
103+
}
104+
}
105+
time++;
106+
}
107+
108+
sb.append("IMPOSSIBLE\n");
109+
}
110+
}
111+
112+
```

0 commit comments

Comments
 (0)