Skip to content

Commit 02420e5

Browse files
authored
Merge pull request #989 from AlgorithmWithGod/Ukj0ng
[20250927] BOJ / P5 / 안대 낀 스피드러너 / 한종욱
2 parents a896efe + ec12981 commit 02420e5

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
// 0: 아래, 1: 오른쪽, 2: 위, 3: 왼쪽
11+
private static int[][] map;
12+
private static boolean[][][][][][] visited;
13+
private static int N;
14+
15+
public static void main(String[] args) throws IOException {
16+
init();
17+
int answer = BFS();
18+
19+
bw.write(answer + "\n");
20+
bw.flush();
21+
bw.close();
22+
br.close();
23+
}
24+
25+
private static void init() throws IOException {
26+
N = Integer.parseInt(br.readLine());
27+
map = new int[N][N];
28+
visited = new boolean[N][N][4][N][N][4];
29+
30+
for (int i = 0; i < N; i++) {
31+
char[] input = br.readLine().toCharArray();
32+
for (int j = 0; j < N; j++) {
33+
if (input[j] == 'H') {
34+
map[i][j] = 1;
35+
}
36+
}
37+
}
38+
}
39+
40+
private static int BFS() {
41+
Queue<int[]> q = new ArrayDeque<>();
42+
int time = 0;
43+
visited[N-1][0][1][N-1][0][2] = true;
44+
q.add(new int[] {N-1, 0, 1, N-1, 0, 2, 0});
45+
46+
while (!q.isEmpty()) {
47+
int[] current = q.poll();
48+
49+
if (current[0] == 0 && current[1] == N-1 && current[3] == 0 && current[4] == N-1) {
50+
time = current[6];
51+
break;
52+
}
53+
54+
int nx1, ny1, nx2, ny2;
55+
int dir1, dir2;
56+
57+
if (current[0] == 0 && current[1] == N-1) {
58+
nx1 = current[0];
59+
ny1 = current[1];
60+
} else {
61+
nx1 = current[0] + dx[current[2]];
62+
ny1 = current[1] + dy[current[2]];
63+
}
64+
65+
if (current[3] == 0 && current[4] == N-1) {
66+
nx2 = current[3];
67+
ny2 = current[4];
68+
} else {
69+
nx2 = current[3] + dx[current[5]];
70+
ny2 = current[4] + dy[current[5]];
71+
}
72+
73+
if (OOB(nx1, ny1) || map[nx1][ny1] == 1) {
74+
nx1 = current[0];
75+
ny1 = current[1];
76+
}
77+
78+
if (OOB(nx2, ny2) || map[nx2][ny2] == 1) {
79+
nx2 = current[3];
80+
ny2 = current[4];
81+
}
82+
83+
if (!visited[nx1][ny1][current[2]][nx2][ny2][current[5]]) {
84+
visited[nx1][ny1][current[2]][nx2][ny2][current[5]] = true;
85+
q.add(new int[] {nx1, ny1, current[2], nx2, ny2, current[5], current[6]+1});
86+
}
87+
88+
dir1 = (current[2]+1) % 4;
89+
dir2 = (current[5]+1) % 4;
90+
91+
if (!visited[current[0]][current[1]][dir1][current[3]][current[4]][dir2]) {
92+
visited[current[0]][current[1]][dir1][current[3]][current[4]][dir2] = true;
93+
q.add(new int[] {current[0], current[1], dir1, current[3], current[4], dir2, current[6]+1});
94+
}
95+
96+
dir1 = current[2]-1 < 0 ? 3 : current[2]-1;
97+
dir2 = current[5]-1 < 0 ? 3 : current[5]-1;
98+
99+
if (!visited[current[0]][current[1]][dir1][current[3]][current[4]][dir2]) {
100+
visited[current[0]][current[1]][dir1][current[3]][current[4]][dir2] = true;
101+
q.add(new int[] {current[0], current[1], dir1, current[3], current[4], dir2, current[6]+1});
102+
}
103+
104+
}
105+
106+
return time;
107+
}
108+
109+
private static boolean OOB(int nx, int ny) {
110+
return nx < 0 || nx > N-1 || ny < 0 || ny > N-1;
111+
}
112+
}
113+
```

0 commit comments

Comments
 (0)