Skip to content

Commit ca02b81

Browse files
authored
Merge pull request #2064 from AlgorithmWithGod/zinnnn37
[20260409] BOJ / G3 / 불! / 김민진
2 parents e7500d0 + ec69d04 commit ca02b81

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

zinnnn37/202604/9 BOJ G3 불!.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.ArrayDeque;
4+
import java.util.Queue;
5+
import java.util.StringTokenizer;
6+
7+
public class BJ_4179_불 {
8+
9+
private static final int[] dx = { -1, 1, 0, 0 };
10+
private static final int[] dy = { 0, 0, -1, 1 };
11+
12+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
14+
private static StringTokenizer st;
15+
16+
private static int R, C;
17+
private static char[][] matrix;
18+
private static Queue<Node> q;
19+
private static boolean[][] visited;
20+
21+
private static class Node {
22+
int x;
23+
int y;
24+
int cnt;
25+
26+
public Node(int x, int y, int cnt) {
27+
this.x = x;
28+
this.y = y;
29+
this.cnt = cnt;
30+
}
31+
32+
}
33+
34+
public static void main(String[] args) throws IOException {
35+
init();
36+
sol();
37+
38+
bw.flush();
39+
bw.close();
40+
br.close();
41+
}
42+
43+
private static void init() throws IOException {
44+
st = new StringTokenizer(br.readLine());
45+
R = Integer.parseInt(st.nextToken());
46+
C = Integer.parseInt(st.nextToken());
47+
48+
matrix = new char[R][C];
49+
visited = new boolean[R][C];
50+
q = new ArrayDeque<>();
51+
for (int i = 0; i < R; i++) {
52+
String line = br.readLine();
53+
for (int j = 0; j < C; j++) {
54+
matrix[i][j] = line.charAt(j);
55+
if (matrix[i][j] == 'J') {
56+
q.offer(new Node(i, j, 0));
57+
visited[i][j] = true;
58+
}
59+
}
60+
}
61+
62+
for (int i = 0; i < R; i++) {
63+
for (int j = 0; j < C; j++) {
64+
if (matrix[i][j] == 'F') {
65+
q.offer(new Node(i, j, -1));
66+
}
67+
}
68+
}
69+
}
70+
71+
private static void sol() throws IOException {
72+
while (!q.isEmpty()) {
73+
Node cur = q.poll();
74+
75+
// 지훈이의 현위치까지 불이 번진 경우
76+
if (cur.cnt >= 0 && matrix[cur.x][cur.y] == 'F') continue;
77+
78+
for (int d = 0; d < 4; d++) {
79+
int nx = cur.x + dx[d];
80+
int ny = cur.y + dy[d];
81+
82+
// 지훈이만
83+
if (OOB(nx, ny) && cur.cnt >= 0) {
84+
bw.write(cur.cnt + 1 + "");
85+
return;
86+
}
87+
88+
if (OOB(nx, ny) || matrix[nx][ny] == 'F' || matrix[nx][ny] == '#') {
89+
continue;
90+
}
91+
92+
// 지훈이 이동 가능 여부
93+
if (cur.cnt >= 0 && visited[nx][ny]) continue;
94+
95+
if (matrix[cur.x][cur.y] == 'F') {
96+
matrix[nx][ny] = 'F';
97+
q.offer(new Node(nx, ny, -1));
98+
} else {
99+
visited[nx][ny] = true;
100+
q.offer(new Node(nx, ny, cur.cnt + 1));
101+
}
102+
}
103+
}
104+
bw.write("IMPOSSIBLE");
105+
}
106+
107+
private static boolean OOB(int x, int y) {
108+
return x < 0 || R <= x || y < 0 || C <= y;
109+
}
110+
111+
}
112+
```

0 commit comments

Comments
 (0)