-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathislands.java
37 lines (34 loc) · 1.15 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
//Name: Nguyen Minh Hieu
//Matric number: A0200814W
import java.util.*;
import java.io.*;
public class islands {
public static char image[][];
//find all the adjacent point can connect to make a island
public static void update(int i, int j) {
if (i >= 0 && i <image.length && j >= 0 && j < image[i].length && (image[i][j] == 'L' ||image[i][j] == 'C')) {
image[i][j] = 'X';
update(i+1,j);
update(i-1,j);
update(i,j+1);
update(i,j-1);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int r = sc.nextInt();
int c = sc.nextInt();
image = new char[r][];
int result = 0;
for (int i = 0; i < r; i ++) image[i] = sc.next().toCharArray();
for (int i = 0; i < r; i ++) {
for (int j = 0; j < c; j++) {
if (image[i][j] == 'L') {
result ++; //only the point cannot make connect => new island
update(i,j);
}
}
}
System.out.println(result);
}
}