-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCode_03_Islands.java
More file actions
57 lines (51 loc) · 1.62 KB
/
Copy pathCode_03_Islands.java
File metadata and controls
57 lines (51 loc) · 1.62 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
54
55
56
57
package algorithm.basic05;
/**
* @Created by mood321
* @Date 2019/11/7 0007
* @Description TODO
*/
public class Code_03_Islands {
public static void main(String[] args) {
int[][] m1 = {{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 0, 1, 1, 1, 0},
{0, 1, 1, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},};
System.out.println(countIslands(m1));
int[][] m2 = {{0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 1, 1, 0},
{0, 1, 1, 1, 0, 0, 0, 1, 0},
{0, 1, 1, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 1, 1, 0, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0},};
System.out.println(countIslands(m2));
}
private static int countIslands(int[][] m1) {
if (m1.length < 1 || m1[0].length < 0) {
return 0;
}
int n = 0;
for (int i = 0; i < m1.length; i++) {
for (int i1 = 0; i1 < m1[0].length; i1++) {
if (m1[i][i1] == 1) {
n++;
infect(m1, i, i1, m1.length, m1[0].length);
}
}
}
return n;
}
public static void infect(int[][] m, int i, int j, int N, int M) {
if(m[i][j]!=1 || j<0|| i<0|| i>=N || j>=M){
return;
}
m[i][j]=2;
infect(m,i-1,j,N,M);
infect(m,i,j-1,N,M);
infect(m,i+1,j,N,M);
infect(m,i,j+1,N,M);
}
}