Super flexible solution java , better than editorial


#1

`public class Solution {

void dfs(int i,int j,int vis[][],char arr[][],int rs,int cs,int[] dx, int[] dy)
{
    vis[i][j]=1;
    for(int t=0;t<4;t++)
    {
        int x=i+dx[t];
        int y=j+dy[t];
        if(x>=0 && x<rs && y>=0 && y<cs && arr[x][y]=='X' && vis[x][y]==0)
        {
            dfs(x,y,vis,arr,rs,cs,dx,dy);
        }
    }
}
public int black(ArrayList<String> A) {
    
   int rs=A.size();
   int cs=A.get(0).length();
   char[][] arr=new char[rs][cs];
   
   for(int i=0;i<A.size();i++)
   {
       String tem=A.get(i);
       for(int j=0;j<tem.length();j++)
       {
           arr[i][j]=tem.charAt(j);
       }
   }
   
   int dx[] = {-1, 0, 1, 0}; 
   int dy[] = {0, 1, 0, -1}; 
   int vis[][]=new int[rs][cs];
   int count=0;
   
   
  //  dfs(0,0,vis,arr,rs,cs,dx,dy);
  for(int i=0;i<rs;i++)
  {
      for(int j=0;j<cs;j++)
      {
          if(vis[i][j]==0 && arr[i][j]=='X')
          {
              dfs(i,j,vis,arr,rs,cs,dx,dy);
              count++;
          }
      }
  }
   return count;
}

}
``.