C++ simple DFS solution


#1
int dirX[4] = {-1, 0, 1, 0};
int dirY[4] = {0, -1, 0, 1};

bool Invalid(int x, int y, int lx, int ly)
{
    return (x<0||y<0||x>=lx||y>=ly);
}
void DFS(vector<string> &A, int x, int y, vector<vector<bool>> &visited, int n, int m)
{
    visited[x][y] = true;
    for(int i=0; i<4; i++)
    {
        int newX = x+dirX[i];
        int newY = y+dirY[i];
        if(!Invalid(newX, newY, n, m)&&(!visited[newX][newY])&&A[newX][newY]=='X')
            DFS(A, newX, newY, visited, n, m);
    }
    return;
}
int Solution::black(vector<string> &A) {
    int n = A.size(), m = A[0].length();
    vector<vector<bool>> visited(n, vector<bool> (m, false));
    int shapes=0;
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++)
        {
            if(A[i][j]=='X'&&!visited[i][j])
            {
                DFS(A, i, j, visited, n, m);
                shapes++;
            }
        }
    }
    return shapes;
}