Flood fill Algorithm | Practice | GeeksforGeeks

An image is represented by a 2-D array of integers, each integer representing the pixel value of the image.
Given a coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill, and a pixel value newColor, "flood fill" the image.
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 as the starting pixel), and so on. Replace the color of all of the aforementioned pixels with the newColor.
Example 1:
Input: image = {{1,1,1},{1,1,0},{1,0,1}},
sr = 1, sc = 1, newColor = 2.
Output: {{2,2,2},{2,2,0},{2,0,1}}
Explanation: From the center of the image 
(with position (sr, sc) = (1, 1)), all 
pixels connected by a path of the same color
as the starting pixel are colored with the new 
color.Note the bottom corner is not colored 2, 
because it is not 4-directionally connected to 
the starting pixel.

 
Your Task:
You don't need to read or print anyhting. Your task is to complete the function floodFill() which takes image, sr, sc and newColor as input paramater and returns the image after flood filling.
 
Expected Time Compelxity: O(n*m)
Expected Space Complexity: O(n*m)
 
0
bokonist6 days ago
C++, Simple recursion with no helper functions. 
TC: O(n*m), 
SC: stack size, O(n*m).
.
.
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) 
{
    // Code here 
    int n= image.size();
    int m= image[0].size();
    if(sr>=n || sr<0 || sc>=m || sc<0 || image[sr][sc]==newColor)
        return image;
        
    int oldColor= image[sr][sc];
    image[sr][sc]= -1;
    
    if(sr-1 >=0 && image[sr-1][sc]==oldColor) floodFill(image, sr-1,sc, newColor);
    if(sr+1 <n && image[sr+1][sc]==oldColor) floodFill(image, sr+1,sc,newColor);
    if(sc-1 >=0 && image[sr][sc-1]==oldColor) floodFill(image, sr,sc-1,newColor);
    if(sc+1 <m && image[sr][sc+1]==oldColor) floodFill(image,sr,sc+1,newColor);
    
    image[sr][sc]= newColor;
    return image;
    
}
0
sharadyaduvanshi2 weeks ago
  bool issafe(vector<vector<int>>& image,int n,int m,int i,int j,vector<vector<int>>&vis,int newColor,int src)   {       if(i < 0 || i >= n || j < 0 || j >= m || image[i][j] != src || vis[i][j] == 1 )       return false;       return true;          }      void helper(vector<vector<int>>& image,int n,int m, int i, int j, int newColor,vector<vector<int>>&vis,int src)   {       if(i == n && j == m)       return ;                 image[i][j] = newColor;            vis[i][j] = 1;                 if(issafe(image,n,m,i,j-1,vis,newColor,src)) //left       {                      helper(image,n,m,i,j-1,newColor,vis,src);       }        if(issafe(image,n,m,i,j+1,vis,newColor,src)) //right       {           helper(image,n,m,i,j+1,newColor,vis,src);       }        if(issafe(image,n,m,i-1,j,vis,newColor,src)) //up       {           helper(image,n,m,i-1,j,newColor,vis,src);       }        if(issafe(image,n,m,i+1,j,vis,newColor,src)) //down       {           helper(image,n,m,i+1,j,newColor,vis,src);       }       vis[i][j] = 0;      // return ;          }      vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {       // Code here              int n = image.size();       int m = image[0].size();              vector<vector<int>>vis(n+1,vector<int>(m+1,0));                     int src = image[sr][sc];       image[sr][sc] = newColor;       helper(image,n,m,sr,sc,newColor,vis,src);       return image;          }
0
naveenapatnaik3 weeks ago
JAVA SOLUTION!!
public int[][] floodFill(int[][] image, int sr, int sc, int newColor)   {       // Code here        int oldColor = image[sr][sc];       if(oldColor == newColor){           return image;       }       fill(image, sr, sc, oldColor, newColor);       return image;   }      public void fill(int[][] image, int sr, int sc, int oldColor,    int newColor){      if(sr<0 || sc<0 || sr>=image.length || sc>=image[0].length ||          image[sr][sc]!=oldColor){           return;       }       image[sr][sc] = newColor;       fill(image, sr-1, sc, oldColor, newColor);       fill(image, sr+1, sc, oldColor, newColor);       fill(image, sr, sc-1, oldColor, newColor);       fill(image, sr, sc+1, oldColor, newColor);      }
0
itvictoralfonsoflores1 month ago
This is my solution, i accept comments.
 
class Solution{   public int[][] floodFill(int[][] image, int sr, int sc, int newColor)   {       int color = image[sr][sc];              if(color == newColor) {           return image;       }              image = recurse(image, sr, sc, color, newColor);              return image;       // Code here           }     private int[][] recurse(int[][] image, int sr, int sc, int oldColor, int newColor)   {       if(image[sr][sc] == oldColor){           image[sr][sc] = newColor;                      if(sr + 1 < image.length)               image = recurse(image,sr+1,sc,oldColor,newColor);           if(sr-1 >= 0)               image = recurse(image,sr-1,sc,oldColor,newColor);           if(sc+1 < image[0].length)               image = recurse(image,sr,sc+1,oldColor,newColor);           if(sc-1 >= 0)               image = recurse(image,sr,sc-1,oldColor,newColor);       }              return image;   } }
0
seagullcode1 month ago
  void solve(vector<vector<int>>& image, int sr, int sc, int newColor, vector<vector<int>> &vis, int init){              if(sr<0 || sr>=image.size() || sc<0 || sc>=image[0].size() || vis[sr][sc] || image[sr][sc]!=init){           return;       }              vis[sr][sc]=1;       image[sr][sc]=newColor;              solve(image,sr+1,sc,newColor,vis,init);       solve(image,sr-1,sc,newColor,vis,init);       solve(image,sr,sc+1,newColor,vis,init);       solve(image,sr,sc-1,newColor,vis,init);                 }   public:   vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {       // Code here        int init = image[sr][sc];       vector<vector<int>> vis(image.size(),vector<int>(image[0].size(),0));       solve(image,sr,sc,newColor,vis,init);              return image;          }};
0
stanislaslegendre1 month ago
simple Java solution
 
class Solution
{
    public int[][] floodFill(int[][] image, int sr, int sc, int newColor)
    {
        int oldColor = image[sr][sc];
        if(oldColor == newColor) return image;
        recurse(image, sr, sc, oldColor, newColor);
        return image;
    }
    
    private void flood(int[][] image, int sr, int sc, int oldColor, int newColor) {
        if(sr > image.length -1 || sc > image[0].length -1 || sr <0 || sc < 0) return;
        if(image[sr][sc] != oldColor) return;
        recurse(image, sr, sc, oldColor, newColor);
        
    }
    
    private void recurse(int[][] image, int sr, int sc, int oldColor, int newColor ) {
        image[sr][sc] = newColor;
        flood(image, sr +1, sc, oldColor, newColor);
        flood(image, sr -1, sc, oldColor, newColor);
        flood(image, sr , sc +1, oldColor, newColor);
        flood(image, sr , sc -1, oldColor, newColor);
    }
}
0
geminicode2 months ago
don't think we need extra space!! here is my python solution
def floodFill(self, image, sr, sc, newColor):
 #Code here
 
 oldc = image[sr][sc]
 move = [[0,1],[1,0],[0,-1],[-1,0]]
 def dfs(r,c):
     if image[r][c] == newColor:
         return
     image[r][c] = newColor
     for i in move:
         if r + i[0] >= 0 and r + i[0] < len(image) and c + i[1] >= 0 and\
         c+ i[1] < len (image[0]) and image[r+i[0]][c+i[1]] == oldc:
             dfs(r+i[0],c+i[1])
    dfs(sr,sc)
    return image
0
agrawalvanisha102 months ago
Someone pls tell the mistake...it is showing segmentation fault, I have already added the condition, if image[sr][sc]==newColor then return.
 
int distx[4]={0,0,1,-1};   int disty[4]={1,-1,0,0};
 
vector<vector<int>> changeColor(vector<vector<int>>& image, int &sr, int &sc, int newColor,int n,int m)   {
       static int givenColor=image[sr][sc];              image[sr][sc]=newColor;              for(int i=0;i<4;i++)       {           if(sr+distx[i]>=0 && sc+disty[i]>=0 && sr+distx[i]<n && sc+disty[i]<m && image[sr+distx[i]][sc+disty[i]]==givenColor)           {               int x=sr+distx[i];               int y=sc+disty[i];               changeColor(image,x,y,newColor,n,m);           }       }       return image;             }
 
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {       // Code here        int n=image.size();       if(n==0)       return image;       int m=image[0].size();       if(image[sr][sc]==newColor)       return image;       return changeColor(image,sr,sc,newColor,n,m);         }  
0
amoghagarwal05032 months ago
Ezz
Ezz
0
bishtkunal092 months ago
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {       queue<pair<int,int>> q;             int r=image.size();       int c=image[0].size();        vector<vector<int>> vis(r,vector<int>(c,0));              q.push({sr,sc});       int p=image[sr][sc];       image[sr][sc]=newColor;                     int dx[]={0,0,1,-1};       int dy[]={1,-1,0,0};       while(!q.empty()){           int k=q.size();           while(k--){               int x=q.front().first;               int y=q.front().second;               q.pop();               image[x][y]=newColor;               for(int i=0;i<4;i++){                   if(x+dx[i]>=r||x+dx[i]<0||y+dy[i]<0||y+dy[i]>=c||vis[x+dx[i]][y+dy[i]]==1){                       continue;                   }                   if(image[x+dx[i]][y+dy[i]]==p){                       vis[x+dx[i]][y+dy[i]]=1;                       q.push({x+dx[i],y+dy[i]});                   }                                                     }           }                  }       return image;          }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.