Created
April 14, 2021 04:24
-
-
Save Neo-sunny/4d86f22064396e8f4d28eedeb1f43f7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int numIslands(char[][] grid) { | |
int vis[][] = new int[301][301]; | |
for(int[] m:vis) Arrays.fill(m,0); | |
int[][] river = new int[grid.length][grid[0].length]; | |
for(int i=0; i<grid.length; i++){ | |
for(int j=0; j<grid[i].length; j++){ | |
int val = grid[i][j]-'0'; | |
river[i][j]=val; | |
} | |
} | |
int count=0; | |
for(int i=0; i<grid.length; i++){ | |
for(int j=0; j<grid[i].length; j++){ | |
if(river[i][j]==1 && vis[i][j]==0){ | |
dfs(river,vis, i,j,grid.length, grid[i].length); | |
count++; | |
} | |
} | |
} | |
// System.out.println(grid.length+" "+grid[0].length); | |
return count; | |
} | |
public void dfs(int[][] grid, int[][] vis, int i,int j,int n,int m){ | |
if(i<0 && j<0 && i>=n && j>=m && grid[i][j]==0) | |
return; | |
int x[]={-1,1,0,0}; | |
int y[]={0,0,1,-1}; | |
vis[i][j]=1; | |
for(int k=0; k<4; k++){ | |
dfs(grid, vis,i+x[i],j+y[i],n,m); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment