forked from algorithm009-class01/algorithm009-class01
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumIslands.java
More file actions
42 lines (38 loc) · 1.12 KB
/
NumIslands.java
File metadata and controls
42 lines (38 loc) · 1.12 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
package serach;
public class NumIslands {
private int n;
private int m;
public int numIslands(char[][] grid){
int count = 0;
n = grid.length;
if (n == 0) return 0;
m = grid[0].length;
for (int i = 0;i<n;i++){
for (int j = 0; j < m; j++) {
if (grid[i][j] == '1'){
dfsMarking(grid,i,j);
++count;
}
}
}
return count;
}
private void dfsMarking(char[][] grid, int i, int j) {
if (i < 0 || j <0 || i>= n || j >=m || grid[i][j] != '1') return;
grid[i][j] ='0';
dfsMarking(grid,i+1,j);
dfsMarking(grid,i-1,j);
dfsMarking(grid,i,j+1);
dfsMarking(grid,i,j-1);
}
public static void main(String[] args) {
char[][] grid = {
{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'}};
NumIslands numIslands = new NumIslands();
int r = numIslands.numIslands(grid);
System.out.println(r);
}
}