733. Flood Fill 🌟

An image is represented by an m x n integer grid image where image[i][j] represents the pixel value of the image.

You are also given three integers sr, sc, and newColor. You should perform a flood fill on the image starting from the pixel image[sr][sc].

To perform a flood fill, consider the starting pixel, plus any pixels connected 4-directionally to the starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those pixels (also with the same color), and so on. Replace the color of all of the aforementioned pixels with newColor.

Return the modified image after performing the flood fill.

DFS - Recursive

Code

class Solution{
public:
    vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int newColor){
        if (image[sr][sc] != newColor)
            dfs(image, sr, sc, newColor, image[sr][sc]);
        return image;
    }

private:
    void dfs(vector<vector<int>> &imgs, int r, int c, int newColor, int oldColor){
        // invalid row or column
        if (r < 0 || c < 0 || r >= imgs.size() || c >= imgs[0].size()) return;
        // current color is not old color or is already new color
        if (imgs[r][c] != oldColor || imgs[r][c] == newColor) return;

        imgs[r][c] = newColor; // for next we can also use dirs vector.
        dfs(imgs, r - 1, c, newColor, oldColor); // up
        dfs(imgs, r + 1, c, newColor, oldColor); // down
        dfs(imgs, r, c - 1, newColor, oldColor); // left
        dfs(imgs, r, c + 1, newColor, oldColor); // right
    }
};

BFS - Iterative

Code

class Solution{
public:
    vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int newColor){
        int currColor = image[sr][sc];
        // if current color is already new color, return original image
        if (currColor == newColor) return image;

        // UNCOMMENT BELOW LINE
        const vector<pair<int, int>> dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // directions vector

        queue<pair<int, int>> q; // queue for bfs
        q.push({sr, sc}); // push starting point
        // while queue is not empty run loop.
        while (!q.empty()){
            // get the row and column index form the front of the queue, and pop it.
            int row = q.front().first, col = q.front().second;
            q.pop();

            // set current color to new color
            image[row][col] = newColor;
            // for all directions push {r,c} in the queue if it's valid.
            for (auto &x : dirs){
                int r = row + x.first, c = col + x.second;
                if (r < 0 || c < 0 || r >= image.size() || c >= image[0].size() || image[r][c] != currColor)
                    continue;
                q.push({r, c});
            }
        }
        return image;
    }
};

MUST READS: