Skip to content

Commit 27f6884

Browse files
authored
Merge pull request #252 from AlgorithmWithGod/khj20006
[20250317] BOJ / G2 / 통나무 옮기기 / 권혁준
2 parents ee271e6 + 5151405 commit 27f6884

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
```java
2+
3+
import java.util.*;
4+
import java.io.*;
5+
6+
public class Main {
7+
8+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
static StringTokenizer st = new StringTokenizer("");
11+
12+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
13+
static int nextInt() throws Exception {
14+
if(!st.hasMoreTokens()) nextLine();
15+
return Integer.parseInt(st.nextToken());
16+
}
17+
static long nextLong() throws Exception {
18+
if(!st.hasMoreTokens()) nextLine();
19+
return Long.parseLong(st.nextToken());
20+
}
21+
static void ioClose() throws Exception {bw.flush();bw.close();br.close();}
22+
23+
/*
24+
* [solving strategy]
25+
* 기차의 왼쪽 위의 점 (x,y)를 잡고, 기차의 방향(dir)을 추가하면
26+
* 각 상태 (x,y,dir)의 관점에서 보면 항상 최단 거리로 각 상태에 도달하는 것이 이득입니다.
27+
* 따라서, 적절히 시작점을 찾아 3차원 필드에서 BFS를 수행합니다.
28+
*
29+
* [description]
30+
* - N : 공간의 한 변의 길이
31+
* - A : 공간의 정보
32+
* - dx, dy : 4방향 탐색을 위한 x, y의 변홧값 배열
33+
*
34+
* - ready() : input 처리
35+
* - solve() : 초기 상태 확인, bfs 호출하여 정답 도출
36+
* - bfs(sx,sy,sdir) : 초기 상태를 (sx,sy,sdir)로 두고 3차원 필드에서 BFS 수행
37+
* - canRotate(x,y,dir) : 주어진 상태 (x,y,dir)에서 올바르게 회전시킬 수 있는지 확인, boolean 형으로 반환
38+
* - isEnd(x,y,dir) : 현재 상태 (x,y,dir)이 목적지의 상태와 동일한지 확인, boolean 형으로 반환
39+
* - safe(x,y,dir) : 상태 (x,y,dir)이 유효한 상태인지 확인, boolean 형으로 반환
40+
*/
41+
42+
static int N;
43+
static char[][] A;
44+
static int[] dx = {1,0,-1,0};
45+
static int[] dy = {0,1,0,-1};
46+
47+
public static void main(String[] args) throws Exception {
48+
//--------------솔루션 코드를 작성하세요.--------------------------------
49+
50+
ready();
51+
solve();
52+
53+
ioClose();
54+
55+
}
56+
57+
static void ready() throws Exception {
58+
59+
N = nextInt();
60+
A = new char[N][];
61+
for(int i=0;i<N;i++) A[i] = br.readLine().toCharArray();
62+
63+
}
64+
65+
static void solve() throws Exception {
66+
67+
for(int i=0;i<N;i++) for(int j=0;j<N;j++) if(A[i][j] == 'B') {
68+
int dir = 1;
69+
if(i<N-1 && A[i+1][j] == 'B') dir = 0;
70+
bw.write(bfs(i,j,dir) + "\n");
71+
return;
72+
}
73+
74+
}
75+
76+
static int bfs(int sx, int sy, int sdir) {
77+
78+
Queue<int[]> Q = new LinkedList<>();
79+
boolean[][][] vis = new boolean[N][N][2];
80+
81+
vis[sx][sy][sdir] = true;
82+
Q.offer(new int[] {sx,sy,sdir,0});
83+
84+
while(!Q.isEmpty()) {
85+
int[] now = Q.poll();
86+
int x = now[0], y = now[1], dir = now[2], t = now[3];
87+
if(isEnd(x,y,dir)) return t;
88+
89+
if(canRotate(x,y,dir)) {
90+
if(dir == 0) {
91+
if(!vis[x+1][y-1][1]) {
92+
vis[x+1][y-1][1] = true;
93+
Q.offer(new int[] {x+1,y-1,1,t+1});
94+
}
95+
}
96+
else {
97+
if(!vis[x-1][y+1][0]) {
98+
vis[x-1][y+1][0] = true;
99+
Q.offer(new int[] {x-1,y+1,0,t+1});
100+
}
101+
}
102+
}
103+
104+
for(int i=0;i<4;i++) {
105+
int xx = x+dx[i], yy = y+dy[i];
106+
if(safe(xx,yy,dir) && !vis[xx][yy][dir]) {
107+
vis[xx][yy][dir] = true;
108+
Q.offer(new int[] {xx,yy,dir,t+1});
109+
}
110+
}
111+
112+
}
113+
114+
return 0;
115+
116+
}
117+
118+
static boolean canRotate(int x, int y, int dir) {
119+
if(dir == 0) {
120+
if(0<=y-1 && y+1<N) {
121+
for(int xx=x;xx<=x+2;xx++) for(int yy=y-1;yy<=y+1;yy++) {
122+
if(A[xx][yy] == '1') return false;
123+
}
124+
}
125+
else return false;
126+
}
127+
else {
128+
if(0<=x-1 && x+1<N) {
129+
for(int xx=x-1;xx<=x+1;xx++) for(int yy=y;yy<=y+2;yy++) {
130+
if(A[xx][yy] == '1') return false;
131+
}
132+
}
133+
else return false;
134+
}
135+
return true;
136+
}
137+
138+
static boolean isEnd(int x, int y, int dir) {
139+
if(A[x][y] != 'E') return false;
140+
if(dir == 0) return A[x+1][y] == 'E' && A[x+2][y] == 'E';
141+
return A[x][y+1] == 'E' && A[x][y+2] == 'E';
142+
}
143+
144+
static boolean safe(int x, int y, int dir) {
145+
boolean res = 0<=x && x<N && 0<=y && y<N && A[x][y] != '1';
146+
if(!res) return res;
147+
if(dir == 0) res &= x+2 < N && A[x+1][y] != '1' && A[x+2][y] != '1';
148+
else res &= y+2 < N && A[x][y+1] != '1' && A[x][y+2] != '1';
149+
return res;
150+
}
151+
152+
}
153+
154+
```

0 commit comments

Comments
 (0)