Skip to content

Commit ee65b32

Browse files
authored
Merge pull request #662 from AlgorithmWithGod/JHLEE325
[20250814] BOJ / G3 / 불! / 이준희
2 parents 36b5b1a + 2cdde3d commit ee65b32

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

JHLEE325/202508/14 BOJ G3 불!.md

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

0 commit comments

Comments
 (0)