-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmazon-IslandCount
More file actions
40 lines (34 loc) · 1.07 KB
/
Copy pathAmazon-IslandCount
File metadata and controls
40 lines (34 loc) · 1.07 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
import java.util.*;
class Solution {
static boolean visited[][];
static int[] ROW=new int[]{0,0,1,-1};
static int[] COL=new int[]{-1,+1,0,0};
public static void main(String args[]) {
char[][] grid = new char[][]{"11110".toCharArray(),
"11010".toCharArray(),
"11000".toCharArray(),
"00000".toCharArray()
};
int m=grid.length;
int n=grid[0].length;
visited=new boolean[m][n];
int count=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]=='1' && visited[i][j]==false){
dfs(grid,i,j);
}
}
}
System.out.println(count);
}
private static void dfs(char[][] grid, int s_i, int s_j) {
visited[s_i][s_j]=true;
for(int i=0;i<4;i++){
int r=ROW[i]+s_i;
int c=COL[i]+s_j;
if(r>=0 && r<grid.length && c>=0 && c<grid[0].length && visited[r][c]==false && grid[r][c]=='1')
dfs(grid,r,c);
}
}
}