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