Skip to content

Commit 6c1cf9e

Browse files
committed
[BFS] baekjoon-1938
1 parent ee60212 commit 6c1cf9e

File tree

4 files changed

+226
-0
lines changed

4 files changed

+226
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
| 10 | | [Baekjoon-14226 이모티콘](./src/BFS/P14226) | |
126126
| 11 | | [Baekjoon-13549 숨바꼭질 3](./src/BFS/P13549) | |
127127
| 12 | ⭐️ | [Baekjoon-20304 비밀번호 제작](./src/BFS/P20304) | |
128+
| 13 | | [Baekjoon-1938 통나무 옮기기](./src/BFS/P1938) | |
128129

129130
## Divide and Conquer
130131

Diff for: src/BFS/P1938/Main.java

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package BFS.P1938;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
final static int col = 0, row = 1;
9+
static int N;
10+
static char[][] map;
11+
static Position start, dest;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/BFS/P1938/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
17+
N = Integer.parseInt(br.readLine());
18+
19+
map = new char[N+2][N+2];
20+
for (int i = 0; i < N+2; i++) Arrays.fill(map[i], '1');
21+
for (int i = 1; i <= N; i++) {
22+
String line = br.readLine();
23+
for (int j = 1; j <= N; j++) {
24+
map[i][j] = line.charAt(j-1);
25+
if (map[i][j] == 'B') start = setPosition(start, i, j);
26+
else if (map[i][j] == 'E') dest = setPosition(dest, i, j);
27+
}
28+
}
29+
30+
System.out.println(bfs());
31+
}
32+
33+
static int bfs() {
34+
Queue<Position> q = new LinkedList<>();
35+
boolean[][][] visited = new boolean[N+2][N+2][2];
36+
int cnt = 0;
37+
38+
q.offer(start);
39+
visited[start.i][start.j][start.d] = true;
40+
while (!q.isEmpty()) {
41+
int curSize = q.size();
42+
while (curSize-- > 0) {
43+
Position cur = q.poll();
44+
if (cur.equals(dest)) return cnt;
45+
46+
Position to;
47+
to = moveUp(cur);
48+
if (to != null && !visited[to.i][to.j][to.d]) {
49+
visited[to.i][to.j][to.d] = true;
50+
q.offer(to);
51+
}
52+
to = moveDown(cur);
53+
if (to != null && !visited[to.i][to.j][to.d]) {
54+
visited[to.i][to.j][to.d] = true;
55+
q.offer(to);
56+
}
57+
to = moveLeft(cur);
58+
if (to != null && !visited[to.i][to.j][to.d]) {
59+
visited[to.i][to.j][to.d] = true;
60+
q.offer(to);
61+
}
62+
to = moveRight(cur);
63+
if (to != null && !visited[to.i][to.j][to.d]) {
64+
visited[to.i][to.j][to.d] = true;
65+
q.offer(to);
66+
}
67+
to = rotate(cur);
68+
if (to != null && !visited[to.i][to.j][to.d]) {
69+
visited[to.i][to.j][to.d] = true;
70+
q.offer(to);
71+
}
72+
}
73+
cnt ++;
74+
}
75+
return 0;
76+
}
77+
78+
static Position moveUp(Position p) {
79+
if (p.d == col) {
80+
if (map[p.i-1][p.j] == '1') return null;
81+
} else if (map[p.i-1][p.j] == '1' || map[p.i-1][p.j+1] == '1' || map[p.i-1][p.j+2] == '1') return null;
82+
return new Position(p.i-1, p.j, p.d);
83+
}
84+
85+
static Position moveDown(Position p) {
86+
if (p.d == col) {
87+
if (map[p.i+3][p.j] == '1') return null;
88+
} else if (map[p.i+1][p.j] == '1' || map[p.i+1][p.j+1] == '1' || map[p.i+1][p.j+2] == '1') return null;
89+
return new Position(p.i+1, p.j, p.d);
90+
}
91+
92+
static Position moveLeft(Position p) {
93+
if (p.d == row) {
94+
if (map[p.i][p.j-1] == '1') return null;
95+
} else if (map[p.i][p.j-1] == '1' || map[p.i+1][p.j-1] == '1' || map[p.i+2][p.j-1] == '1') return null;
96+
return new Position(p.i, p.j-1, p.d);
97+
}
98+
99+
static Position moveRight(Position p) {
100+
if (p.d == row) {
101+
if (map[p.i][p.j+3] == '1') return null;
102+
} else if (map[p.i][p.j+1] == '1' || map[p.i+1][p.j+1] == '1' || map[p.i+2][p.j+1] == '1') return null;
103+
return new Position(p.i, p.j+1, p.d);
104+
}
105+
106+
static Position rotate(Position p) {
107+
if (p.d == col) {
108+
if (map[p.i][p.j-1] == '1' || map[p.i][p.j+1] == '1' ||
109+
map[p.i+1][p.j-1] == '1' || map[p.i+1][p.j+1] == '1' ||
110+
map[p.i+2][p.j-1] == '1' || map[p.i+2][p.j+1] == '1') {
111+
return null;
112+
}
113+
return new Position(p.i+1, p.j-1, row);
114+
} else {
115+
if (map[p.i-1][p.j] == '1' || map[p.i+1][p.j] == '1' ||
116+
map[p.i-1][p.j+1] == '1' || map[p.i+1][p.j+1] == '1' ||
117+
map[p.i-1][p.j+2] == '1' || map[p.i+1][p.j+2] == '1') {
118+
return null;
119+
}
120+
return new Position(p.i-1, p.j+1, col);
121+
}
122+
}
123+
124+
static Position setPosition(Position p, int i, int j) {
125+
if (p == null) p = new Position(i, j, 0);
126+
else if (p.j == j) p.d = col;
127+
else p.d = row;
128+
return p;
129+
}
130+
131+
static class Position {
132+
int i, j, d;
133+
134+
public Position(int i, int j, int d) {
135+
this.i = i;
136+
this.j = j;
137+
this.d = d;
138+
}
139+
140+
@Override
141+
public boolean equals(Object o) {
142+
Position p = (Position) o;
143+
return i == p.i && j == p.j && d == p.d;
144+
}
145+
}
146+
}

Diff for: src/BFS/P1938/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## [baekjoon-1938] 통나무 옮기기
2+
3+
![image](https://user-images.githubusercontent.com/22045163/110050117-94cd9700-7d96-11eb-9c90-ab73c8cd29e1.png)
4+
![image](https://user-images.githubusercontent.com/22045163/110050138-a2831c80-7d96-11eb-803b-3e351f2d6560.png)
5+
6+
![image](https://user-images.githubusercontent.com/22045163/110050165-b3cc2900-7d96-11eb-85b4-334c9fdbbf0d.png)

Diff for: src/BFS/P1938/input.txt

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
5
2+
BBB00
3+
00000
4+
10E00
5+
01E00
6+
00E00
7+
//5
8+
9+
7
10+
1000111
11+
0000000
12+
B000000
13+
B00100E
14+
B00110E
15+
000100E
16+
1000110
17+
//11
18+
19+
7
20+
0000000
21+
0E00000
22+
0E00BBB
23+
0E01001
24+
0000000
25+
0000001
26+
0100000
27+
//5
28+
29+
7
30+
0001000
31+
0001000
32+
B00100E
33+
B00000E
34+
B00100E
35+
0001000
36+
0001000
37+
//8
38+
39+
5
40+
B0011
41+
B0000
42+
B0000
43+
01000
44+
EEE10
45+
//0
46+
47+
4
48+
BBB0
49+
0E00
50+
0E00
51+
0E00
52+
//3
53+
54+
4
55+
0B00
56+
EB00
57+
EB00
58+
E000
59+
//2
60+
61+
4
62+
000E
63+
BBBE
64+
000E
65+
0000
66+
//3
67+
68+
5
69+
B0011
70+
B0000
71+
B0000
72+
11000
73+
EEE00

0 commit comments

Comments
 (0)