-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq16p19.java
More file actions
40 lines (37 loc) · 1.12 KB
/
Copy pathq16p19.java
File metadata and controls
40 lines (37 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
import java.util.*;
public class q16p19{
public static void main(String[] args){
int[][] input = {{0,2,1,2},
{0,1,0,1},
{1,1,0,1},
{0,1,0,1}};
//List<Integer> li = new ArrayList<>();
System.out.println(pond(input).toString());
}
public static List<Integer> pond(int[][] input){
List<Integer> li = new ArrayList<>();
int cnt = 0;
for(int i = 0; i<input.length; i++){
for(int j = 0; j<input[0].length; j++){
if(input[i][j] == 0){
cnt = count(input, i, j);
li.add(cnt);
}
}
}
return li;
}
public static int count(int[][] input, int x, int y){
if(x<0 || y<0 || y>=input[0].length || x>=input.length ||input[x][y] != 0 ||input[x][y] == -1){
return 0;
}
int cnt = 1;
input[x][y] = -1;
for(int i=x-1; i<=x+1; i++){
for(int j=y-1; j<=y+1; j++){
cnt += count(input, i, j);
}
}
return cnt;
}
}