MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / dfs

Method dfs

CountSubIsIland.java:5–22  ·  view source on GitHub ↗
(int row, int col,int[][] grid1, int[][] grid2,
    boolean visited[][])

Source from the content-addressed store, hash-verified

3 int cols;
4 int dirs[][] = {{-1,0},{0,1},{1,0},{0,-1}};
5 public boolean dfs(int row, int col,int[][] grid1, int[][] grid2,
6 boolean visited[][]){
7 visited[row][col] = true;
8 boolean isIsland = true;
9 if(grid1[row][col]==0){
10 isIsland = false;
11 }
12 //visiting neighbour cells;
13 for(int dir[] : dirs){
14 int nextR = row + dir[0];
15 int nextC = col + dir[1];
16 if(nextR>=0 && nextC>=0 && nextR<rows && nextC<cols && grid2[nextR][nextC]==1 && !visited[nextR][nextC]){
17 boolean res = dfs(nextR,nextC,grid1,grid2,visited);
18 isIsland = isIsland && res;
19 }
20 }
21 return isIsland;
22 }
23 public int countSubIslands(int[][] grid1, int[][] grid2) {
24 rows = grid1.length;
25 cols = grid1[0].length;

Callers 1

countSubIslandsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected