MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / Islands

Class Islands

GeeksForGeeks/Number of islands/Islands.java:1–36  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Islands {
2
3 // Function to find the number of island in the given list A
4 // N, M: size of list row and column respectively
5 static int findIslands(ArrayList<ArrayList<Integer>> A, int N, int M) {
6 // we will use dfs to traverse graph
7 // Your code here
8 int count=0;
9 for(int i=0;i<N;i++){
10 for(int j=0;j<M;j++){
11 if(A.get(i).get(j)==1){
12 count++;
13 dfsUtil(A,i,j);
14 }
15 }
16 }
17 return count;
18 }
19
20 static void dfsUtil(ArrayList<ArrayList<Integer>> A, int i, int j){
21 if(i<0 || i>=A.size() || j<0 || j>=A.get(i).size() || A.get(i).get(j)==0) // these are the base condiation
22 return ;
23 //we will try to cover all direction
24 //we use the base condition in any DFS opertion i or j valuw will be negative so it is not possible so we will
25 //-return nothing
26 A.get(i).set(j,0);
27 dfsUtil(A,i+1,j);
28 dfsUtil(A,i-1,j);
29 dfsUtil(A,i,j+1);
30 dfsUtil(A,i,j-1);
31 dfsUtil(A,i+1,j-1);
32 dfsUtil(A,i+1,j+1);
33 dfsUtil(A,i-1,j-1);
34 dfsUtil(A,i-1,j+1);
35 }
36}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected