-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bakery.java
61 lines (45 loc) · 1.83 KB
/
Bakery.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
60
61
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Bakery {
private static char[][] resultArr;
private static final char BUILDING_SPACE = 'x';
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
int R = Integer.parseInt(st.nextToken()); // height
int C = Integer.parseInt(st.nextToken()); // width
initResultArr(R, C);
for(int height = 0; height < R; height++) {
char[] infoArr = br.readLine().toCharArray();
mkResultArr(infoArr, height);
}
int count = 0;
for(int height = 0; height < resultArr.length; height++) {
if(dfs(height, 0)) count++;
}
System.out.println(count);
}
private static void initResultArr(int height, int width) {
resultArr = new char[height][width];
}
private static void mkResultArr(char[] infoArr, int height) {
for(int index = 0; index < infoArr.length; index++) {
resultArr[height][index] = infoArr[index];
}
}
private static boolean dfs(int height, int width) {
resultArr[height][width] = BUILDING_SPACE;
if(width == resultArr[0].length-1) return true;
if(height - 1 >= 0 && resultArr[height - 1][width + 1] != BUILDING_SPACE) {
if(dfs(height - 1, width + 1)) return true;
}
if(resultArr[height][width + 1] != BUILDING_SPACE) {
if(dfs(height, width + 1)) return true;
}
if(height + 1 < resultArr.length && resultArr[height + 1][width + 1] != BUILDING_SPACE) {
if(dfs(height + 1, width + 1)) return true;
}
return false;
}
}