Skip to content

Commit c225d54

Browse files
committed
[DFS] baekjoon-3109
1 parent 6a91a86 commit c225d54

File tree

4 files changed

+10076
-0
lines changed

4 files changed

+10076
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
| 10 | | [Baekjoon-4963 섬의 개수](./src/DFS/P4963) | |
8989
| 11 | | [Baekjoon-13023 ABCDE](./src/DFS/P13023) | |
9090
| 12 | | [SWEA-5215 햄버거 다이어트](./src/DFS/swea5215) | |
91+
| 13 | | [Baekjoon-3109 빵집](./src/DFS/P3109) | |
9192

9293
### BFS
9394

src/DFS/P3109/Main.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package DFS.P3109;
2+
3+
import java.io.*;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int R, C, cnt;
9+
static char[][] map;
10+
static boolean flag = false;
11+
12+
public static void main(String[] args) throws Exception {
13+
System.setIn(new FileInputStream("src/DFS/P3109/input.txt"));
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
StringTokenizer st = new StringTokenizer(br.readLine());
16+
17+
R = Integer.parseInt(st.nextToken());
18+
C = Integer.parseInt(st.nextToken());
19+
20+
map = new char[R][C];
21+
for (int i = 0; i < R; i++) {
22+
String line = br.readLine();
23+
for (int j = 0; j < C; j++) {
24+
map[i][j] = line.charAt(j);
25+
}
26+
}
27+
28+
for (int i = 0; i < R; i++) {
29+
map[i][0] = 'p';
30+
flag = false;
31+
dfs(i, 0);
32+
}
33+
34+
System.out.println(cnt);
35+
}
36+
37+
static void dfs(int i, int j) {
38+
if (j == C-1) {
39+
cnt ++;
40+
flag = true;
41+
return;
42+
}
43+
44+
for (int d = -1; d <= 1; d++) {
45+
int to_i = i + d;
46+
int to_j = j + 1;
47+
48+
if (canGo(to_i, to_j) && !flag) {
49+
map[to_i][to_j] = 'p';
50+
dfs(to_i, to_j);
51+
}
52+
}
53+
}
54+
55+
static boolean canGo(int i, int j) {
56+
return 0 <= i && i < R && 0 <= j && j < C && map[i][j] == '.';
57+
}
58+
}

src/DFS/P3109/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-3109] 빵집
2+
3+
![image](https://user-images.githubusercontent.com/22045163/108316208-efe08500-71ff-11eb-8b66-d29e1e2b98dc.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/108316234-fc64dd80-71ff-11eb-931e-fc309352fffe.png)

0 commit comments

Comments
 (0)