Skip to content

Commit 7379760

Browse files
committed
[BFS] baekjoon-2206
1 parent 2e7f73d commit 7379760

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

Diff for: โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
| 15 | | [SWEA-1953 ํƒˆ์ฃผ๋ฒ” ๊ฒ€๊ฑฐ](./src/BFS/swea1953) | |
137137
| 16 | | [SWEA-10966 ๋ฌผ๋†€์ด๋ฅผ ๊ฐ€์ž](./src/BFS/swea10966) | |
138138
| 17 | | [SWEA-1238 Contact](./src/BFS/swea1238) | |
139+
| 18 | | [Baekjoon-2206 ๋ฒฝ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๊ธฐ](./src/BFS/P2206) | |
139140

140141
## Divide and Conquer
141142

Diff for: โ€Žsrc/BFS/P2206/Main.java

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package BFS.P2206;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N, M;
9+
static char[][] map;
10+
static int[] di = {-1, 0, 1, 0};
11+
static int[] dj = {0, 1, 0, -1};
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/BFS/P2206/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
21+
map = new char[N][M];
22+
for (int i = 0; i < N; i++) {
23+
map[i] = br.readLine().toCharArray();
24+
}
25+
26+
System.out.println(bfs());
27+
}
28+
29+
static int bfs() {
30+
Queue<Pos> q = new LinkedList<>();
31+
boolean[][][] visited = new boolean[N][M][2];
32+
33+
q.offer(new Pos(0, 0, 1, 1));
34+
visited[0][0][1] = true;
35+
while (!q.isEmpty()) {
36+
Pos p = q.poll();
37+
if (p.i == N-1 && p.j == M-1) return p.dist;
38+
39+
for (int k = 0; k < 4; k++) {
40+
int to_i = p.i + di[k];
41+
int to_j = p.j + dj[k];
42+
43+
if (!isValidPath(to_i, to_j)) continue;
44+
if (map[to_i][to_j] == '0' && !visited[to_i][to_j][p.wall]) {
45+
visited[to_i][to_j][p.wall] = true;
46+
q.offer(new Pos(to_i, to_j, p.dist+1, p.wall));
47+
} else if (p.wall > 0) {
48+
visited[to_i][to_j][0] = true;
49+
q.offer(new Pos(to_i, to_j, p.dist+1, p.wall-1));
50+
}
51+
}
52+
}
53+
54+
return -1;
55+
}
56+
57+
static boolean isValidPath(int i, int j) {
58+
return 0 <= i && i < N && 0 <= j && j < M;
59+
}
60+
61+
static class Pos {
62+
int i, j, dist, wall;
63+
64+
public Pos(int i, int j, int dist, int wall) {
65+
this.i = i;
66+
this.j = j;
67+
this.dist = dist;
68+
this.wall = wall;
69+
}
70+
}
71+
}

Diff for: โ€Žsrc/BFS/P2206/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-2206] ๋ฒฝ ๋ถ€์ˆ˜๊ณ  ์ด๋™ํ•˜๊ธฐ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/112110431-3a399500-8bf6-11eb-816c-413bd9e74978.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/112110467-44f42a00-8bf6-11eb-9a9a-d189d0873534.png)

Diff for: โ€Žsrc/BFS/P2206/input.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
6 4
2+
0100
3+
1110
4+
1000
5+
0000
6+
0111
7+
0000

0 commit comments

Comments
ย (0)