Skip to content

Commit ddeaf98

Browse files
authored
[20251024] BOJ / G2 / 아맞다우산 / 한종욱
1 parent 2ff3fdf commit ddeaf98

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
```
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
private static final int[] dx = {1, 0, -1, 0};
9+
private static final int[] dy = {0, 1, 0, -1};
10+
private static char[][] map;
11+
private static boolean[][][] visited;
12+
private static int[] start, end;
13+
private static int N, M, count;
14+
public static void main(String[] args) throws IOException {
15+
init();
16+
int answer = BFS();
17+
18+
bw.write(answer + "\n");
19+
bw.flush();
20+
bw.close();
21+
br.close();
22+
}
23+
24+
private static void init() throws IOException {
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
N = Integer.parseInt(st.nextToken());
27+
M = Integer.parseInt(st.nextToken());
28+
29+
map = new char[M][N];
30+
start = new int[2];
31+
end = new int[2];
32+
33+
for (int i = 0; i < M; i++) {
34+
map[i] = br.readLine().toCharArray();
35+
for (int j = 0; j < N; j++) {
36+
if (map[i][j] == 'S') {
37+
start[0] = i;
38+
start[1] = j;
39+
} else if (map[i][j] == 'E') {
40+
end[0] = i;
41+
end[1] = j;
42+
} else if (map[i][j] == 'X') {
43+
map[i][j] = (char) (count + '0');
44+
count++;
45+
}
46+
}
47+
}
48+
49+
visited = new boolean[M][N][1 << count];
50+
}
51+
52+
private static int BFS() {
53+
Queue<int[]> q = new ArrayDeque<>();
54+
int result = 0;
55+
visited[start[0]][start[1]][0] = true;
56+
q.add(new int[]{start[0], start[1], 0, 0});
57+
58+
while (!q.isEmpty()) {
59+
int[] current = q.poll();
60+
61+
if (current[0] == end[0] && current[1] == end[1] && current[2] == (1 << count)-1) {
62+
result = current[3];
63+
break;
64+
}
65+
66+
for (int i = 0; i < 4; i++) {
67+
int nx = current[0] + dx[i];
68+
int ny = current[1] + dy[i];
69+
70+
if (OOB(nx, ny) || map[nx][ny] == '#') continue;
71+
72+
if (!isThings(nx, ny) && !visited[nx][ny][current[2]]) {
73+
visited[nx][ny][current[2]] = true;
74+
q.add(new int[]{nx, ny, current[2], current[3]+1});
75+
} else if (isThings(nx, ny)) {
76+
int v = 1 << (map[nx][ny] - '0');
77+
if (visited[nx][ny][current[2] | v]) continue;
78+
visited[nx][ny][current[2] | v] = true;
79+
q.add(new int[]{nx, ny, current[2] | v, current[3] + 1});
80+
}
81+
82+
}
83+
}
84+
85+
return result;
86+
}
87+
88+
private static boolean OOB(int nx, int ny) {
89+
return nx < 0 || nx > M-1 || ny < 0 || ny > N-1;
90+
}
91+
92+
private static boolean isThings(int nx, int ny) {
93+
return map[nx][ny] != '#' &&
94+
map[nx][ny] != 'S' &&
95+
map[nx][ny] != 'E' &&
96+
map[nx][ny] != '.';
97+
}
98+
}
99+
```

0 commit comments

Comments
 (0)