xxxxxxxxxx
First line contains two space separated integers m, n that denotes the number of rows and columns in the matrix respectively.
Each of the next m lines contain n space-separated integers representing the matrix.
xxxxxxxxxx
class Result {
static int countIslands(int mat[][], int m, int n){
// Write your code here
int ans = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mat[i][j] == 1){
dfs(i,j,mat);
ans++;
}
}
}
return ans;
}
static void dfs(int i,int j,int[][] mat){
if(i<0 || i==mat.length || j<0 || j==mat[0].length ||mat[i][j] == 0){
return;
}
//System.out.println(i+ " " + j );
mat[i][j] = 0;
dfs(i-1,j,mat);
dfs(i,j+1,mat);
dfs(i+1,j,mat);
dfs(i,j-1,mat);
}
}
xxxxxxxxxx
object Solution {
def numIslands(islands: Array[Array[String]]): Int = {
// write your code here
-1
}
}