Easy dfs application c++ sol. similar to island problem


#1
vector<pair<int,int>> step={make_pair(1,0),make_pair(0,1),make_pair(-1,0),make_pair(0,-1)};
bool isSafe(int i,int j,int row,int col)
{
    return (i>=0)&&(i<row)&&(j>=0)&&(j<col);
}
void dfs(int i,int j,vector<string> &A,vector<vector<bool>> &visited)
{
    visited[i][j]=true;
    for(int k=0;k<4;k++)
    {
        int n_x=i+step[k].first,n_y=j+step[k].second;
        if((isSafe(n_x,n_y,A.size(),A[0].size()))&&(A[n_x][n_y]=='X')&&(!visited[n_x][n_y]))
            dfs(n_x,n_y,A,visited);
    }
}
int Solution::black(vector<string> &A) {
    vector<vector<bool>> visited(A.size(),vector<bool> (A[0].size(),false));
    int count=0;
    for(int i=0;i<A.size();i++)
    {
        for(int j=0;j<A[0].size();j++)
        {
            if(A[i][j]=='X'&&!visited[i][j])
            {
                dfs(i,j,A,visited);
                count++;
            }
        }
    }
    return count;
}