CCCPaste

Untitled

 class Solution {
public:
    bool visited[55][55];
    vector> mat;
    const int di[4] = {0, 0, -1, 1};
    const int dj[4] = {-1, 1, 0, 0};
    int s_color, col, n, m;
    void dfs(int ci, int cj) {
        for(int i = 0; i < 4; i++) {
            int ti = ci + di[i];
            int tj = cj + dj[i];
            if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] == s_color) {
                visited[ti][tj] = true;
                mat[ti][tj] = col;
                dfs(ti, tj);
            }
        }
    }
    
    vector> floodFill(vector>& image, int sr, int sc, int color) {
        
        mat = image;
        n = image.size();
        m = image[0].size();
        s_color = image[sr][sc];
        col = color;
        dfs(sr, sc);
        mat[sr][sc] = col;
        return mat;
    }
};