-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeach.java
More file actions
53 lines (47 loc) · 1.77 KB
/
Copy pathBeach.java
File metadata and controls
53 lines (47 loc) · 1.77 KB
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
package algorithm.baek.graph;
import algorithm.TestCase;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.StringTokenizer;
public class Beach implements TestCase {
static int[][] dirsOdd = new int[][]{{-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, 0}, {1, 1}};
static int[][] dirsEven = new int[][]{{-1, -1}, {-1, 0}, {0, -1}, {0, 1}, {1, -1}, {1, 0}};
static boolean[][] visited;
static char[][] beach;
static int count;
@Override
public void test() throws ParseException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
beach = new char[n][m];
visited = new boolean[n][m];
for (int i = 0; i < n; i++) {
beach[i] = br.readLine().toCharArray();
}
count = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (!visited[i][j] && beach[i][j] == '#') {
int[][] dirs = i % 2 == 0 ? dirsEven : dirsOdd;
bfs(i, j, dirs);
}
}
}
System.out.println(count);
}
private void bfs(int i, int j, int[][] dirs) {
visited[i][j] = true;
for (int[] dir : dirs) {
int nextX = i + dir[0];
int nextY = j + dir[1];
if (nextX < 0 || nextX >= beach.length || nextY < 0 || nextY >= beach[0].length) continue;
if (beach[nextX][nextY] != '#') {
count++;
}
}
}
}