Skip to content

Commit 0ed1393

Browse files
authored
[20250713] BOJ / G3 / 불! / 설진영
1 parent e6af756 commit 0ed1393

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Seol-JY/202507/13 BOJ G3 불!.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int R, C;
7+
static char[][] map;
8+
static int[][] fireTime;
9+
static int[][] personTime;
10+
static int[] dx = {-1, 1, 0, 0};
11+
static int[] dy = {0, 0, -1, 1};
12+
13+
public static void main(String[] args) throws IOException {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
R = Integer.parseInt(st.nextToken());
18+
C = Integer.parseInt(st.nextToken());
19+
20+
map = new char[R][C];
21+
fireTime = new int[R][C];
22+
personTime = new int[R][C];
23+
24+
Deque<int[]> fireQueue = new ArrayDeque<>();
25+
Deque<int[]> personQueue = new ArrayDeque<>();
26+
27+
for (int i = 0; i < R; i++) {
28+
Arrays.fill(fireTime[i], -1);
29+
Arrays.fill(personTime[i], -1);
30+
}
31+
32+
for (int i = 0; i < R; i++) {
33+
String line = br.readLine();
34+
for (int j = 0; j < C; j++) {
35+
map[i][j] = line.charAt(j);
36+
if (map[i][j] == 'F') {
37+
fireQueue.offer(new int[]{i, j});
38+
fireTime[i][j] = 0;
39+
} else if (map[i][j] == 'J') {
40+
personQueue.offer(new int[]{i, j});
41+
personTime[i][j] = 0;
42+
}
43+
}
44+
}
45+
46+
while (!fireQueue.isEmpty()) {
47+
int[] current = fireQueue.poll();
48+
int x = current[0];
49+
int y = current[1];
50+
51+
for (int i = 0; i < 4; i++) {
52+
int nx = x + dx[i];
53+
int ny = y + dy[i];
54+
55+
if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
56+
if (map[nx][ny] == '#' || fireTime[nx][ny] != -1) continue;
57+
58+
fireTime[nx][ny] = fireTime[x][y] + 1;
59+
fireQueue.offer(new int[]{nx, ny});
60+
}
61+
}
62+
63+
while (!personQueue.isEmpty()) {
64+
int[] current = personQueue.poll();
65+
int x = current[0];
66+
int y = current[1];
67+
68+
if (x == 0 || x == R-1 || y == 0 || y == C-1) {
69+
System.out.println(personTime[x][y] + 1);
70+
return;
71+
}
72+
73+
for (int i = 0; i < 4; i++) {
74+
int nx = x + dx[i];
75+
int ny = y + dy[i];
76+
77+
if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue;
78+
if (map[nx][ny] == '#' || personTime[nx][ny] != -1) continue;
79+
80+
int nextTime = personTime[x][y] + 1;
81+
82+
if (fireTime[nx][ny] == -1 || fireTime[nx][ny] > nextTime) {
83+
personTime[nx][ny] = nextTime;
84+
personQueue.offer(new int[]{nx, ny});
85+
}
86+
}
87+
}
88+
89+
System.out.println("IMPOSSIBLE");
90+
}
91+
}
92+
```

0 commit comments

Comments
 (0)