-
Notifications
You must be signed in to change notification settings - Fork 0
/
Islands.java
67 lines (58 loc) · 1.96 KB
/
Islands.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
62
63
64
65
66
67
import java.util.LinkedList;
public class Islands {
static char[][] matrix = new char[0][0];
static boolean[][] visited = new boolean[0][0];
public static void main(String[] args) {
Kattio sc = new Kattio(System.in, System.out);
int rows = sc.getInt();
int columns = sc.getInt();
int res = 0;
matrix = new char[rows][columns];
visited = new boolean[rows][columns];
for (int n = 0; n < rows; n++) {
String line = sc.getWord();
for (int m = 0; m < columns; m++) {
matrix[n][m] = line.charAt(m);
}
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (matrix[i][j] == 'L' && !visited[i][j]) {
res++;
visited[i][j] = true;
bfs(i, j, rows, columns);
}
}
}
sc.println(res);
sc.close();
}
public static void bfs(int i, int j, int xmax, int ymax) {
LinkedList<IntegerPair> q = new LinkedList<>();
int[] xdir = { 1, 0, -1, 0 };
int[] ydir = { 0, -1, 0, 1 };
q.add(new IntegerPair(i, j));
while (!q.isEmpty()) {
IntegerPair u = q.removeFirst();
for (i = 0; i < 4; i++) {
int xcord = u.first - xdir[i];
int ycord = u.second - ydir[i];
if (xcord >= 0 && xcord < xmax && ycord >= 0 && ycord < ymax) {
char c = matrix[xcord][ycord];
if (!visited[xcord][ycord] && (c == 'C' || c == 'L')) {
visited[xcord][ycord] = true;
q.add(new IntegerPair(xcord, ycord));
}
}
}
}
}
}
class IntegerPair {
int first;
int second;
public IntegerPair(int first, int second) {
this.first = first;
this.second = second;
}
}