Skip to content

Commit 526f1c6

Browse files
authored
[20260108] BOJ / P5 / 안대 낀 스피드러너 / 이강현
1 parent 1a4be71 commit 526f1c6

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main {
6+
static class State {
7+
int x1, y1, dir1;
8+
int x2, y2, dir2;
9+
int steps;
10+
11+
State(int x1, int y1, int dir1, int x2, int y2, int dir2, int steps) {
12+
this.x1 = x1;
13+
this.y1 = y1;
14+
this.dir1 = dir1;
15+
this.x2 = x2;
16+
this.y2 = y2;
17+
this.dir2 = dir2;
18+
this.steps = steps;
19+
}
20+
}
21+
22+
static int N;
23+
static char[][] map;
24+
static int[] dx = {-1, 0, 1, 0};
25+
static int[] dy = {0, 1, 0, -1};
26+
27+
public static void main(String[] args) throws Exception {
28+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29+
N = Integer.parseInt(br.readLine());
30+
map = new char[N][N];
31+
32+
for (int i = 0; i < N; i++) {
33+
String line = br.readLine();
34+
for (int j = 0; j < N; j++) {
35+
map[i][j] = line.charAt(j);
36+
}
37+
}
38+
39+
System.out.println(bfs());
40+
}
41+
42+
public static int bfs() {
43+
Queue<State> q = new LinkedList<>();
44+
boolean[][][][][][] visited = new boolean[N][N][4][N][N][4];
45+
46+
State start = new State(N-1, 0, 0, N-1, 0, 1, 0);
47+
q.offer(start);
48+
visited[N-1][0][0][N-1][0][1] = true;
49+
50+
while (!q.isEmpty()) {
51+
State cur = q.poll();
52+
53+
if (cur.x1 == 0 && cur.y1 == N-1 && cur.x2 == 0 && cur.y2 == N-1) {
54+
return cur.steps;
55+
}
56+
57+
int nx1 = cur.x1;
58+
int ny1 = cur.y1;
59+
int nx2 = cur.x2;
60+
int ny2 = cur.y2;
61+
62+
if (!(cur.x1 == 0 && cur.y1 == N-1)) {
63+
nx1 = cur.x1 + dx[cur.dir1];
64+
ny1 = cur.y1 + dy[cur.dir1];
65+
if (!isValid(nx1, ny1)) {
66+
nx1 = cur.x1;
67+
ny1 = cur.y1;
68+
}
69+
}
70+
71+
if (!(cur.x2 == 0 && cur.y2 == N-1)) {
72+
nx2 = cur.x2 + dx[cur.dir2];
73+
ny2 = cur.y2 + dy[cur.dir2];
74+
if (!isValid(nx2, ny2)) {
75+
nx2 = cur.x2;
76+
ny2 = cur.y2;
77+
}
78+
}
79+
80+
if (!visited[nx1][ny1][cur.dir1][nx2][ny2][cur.dir2]) {
81+
visited[nx1][ny1][cur.dir1][nx2][ny2][cur.dir2] = true;
82+
q.offer(new State(nx1, ny1, cur.dir1, nx2, ny2, cur.dir2, cur.steps + 1));
83+
}
84+
85+
int newDir1 = (cur.dir1 + 3) % 4;
86+
int newDir2 = (cur.dir2 + 3) % 4;
87+
88+
if (!visited[cur.x1][cur.y1][newDir1][cur.x2][cur.y2][newDir2]) {
89+
visited[cur.x1][cur.y1][newDir1][cur.x2][cur.y2][newDir2] = true;
90+
q.offer(new State(cur.x1, cur.y1, newDir1, cur.x2, cur.y2, newDir2, cur.steps + 1));
91+
}
92+
93+
newDir1 = (cur.dir1 + 1) % 4;
94+
newDir2 = (cur.dir2 + 1) % 4;
95+
96+
if (!visited[cur.x1][cur.y1][newDir1][cur.x2][cur.y2][newDir2]) {
97+
visited[cur.x1][cur.y1][newDir1][cur.x2][cur.y2][newDir2] = true;
98+
q.offer(new State(cur.x1, cur.y1, newDir1, cur.x2, cur.y2, newDir2, cur.steps + 1));
99+
}
100+
}
101+
102+
return -1;
103+
}
104+
105+
static boolean isValid(int x, int y) {
106+
return x >= 0 && x < N && y >= 0 && y < N && map[x][y] == 'E';
107+
}
108+
}
109+
```

0 commit comments

Comments
 (0)