-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP042_BJ3109_빵집.java
59 lines (42 loc) · 1.34 KB
/
P042_BJ3109_빵집.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
59
import java.io.BufferedReader;
import java.io.InputStreamReader;
// dfs
public class P042_BJ3109_빵집 {
static int[] dx = {-1, 0, 1}; // 오른쪽 위 대각선, 오른쪽, 오른쪽 아래 대각선
static int[] dy = {1, 1, 1};
static int R, C;
static char[][] map;
static boolean[][] visited;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
R = Integer.parseInt(input[0]);
C = Integer.parseInt(input[1]);
map = new char[R][C];
for (int i = 0; i < R; i++) {
String s = br.readLine();
for (int j = 0; j < C; j++) {
map[i][j] = s.charAt(j);
}
}
visited = new boolean[R][C];
int count = 0;
for (int i = 0; i < R; i++) { // 0 column 시작 모두 탐색
if (dfs(i, 0)) count++;
}
System.out.println(count);
}
static boolean dfs(int x, int y) {
for (int i = 0; i < 3; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || nx >= R || ny < 0 || ny >= C) continue; // 범위 밖
if (visited[nx][ny] == false && map[nx][ny] == '.') {
visited[nx][ny] = true;
if (ny == C - 1) return true; // 마지막 열에 도달했을 경우
if (dfs(nx, ny)) return true; // 다음 탐색도 전부 true
}
}
return false;
}
}