Skip to content

Commit

Permalink
Added Flood Fill Problem Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jyoti-bhasin committed Oct 30, 2022
1 parent b551eac commit 0a8c17c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions leetcode/Flood_fill.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:

void dfs(int sr,int sc,vector<vector<int>> &ans,vector<vector<int>>& image,int newcolor, int delrow[],int delcol[], int iniColor)
{
int n=image.size();
int m=image[0].size();
ans[sr][sc]=newcolor;
for(int i=0;i<4;i++)
{
int nrow= sr+delrow[i];
int ncol= sc+delcol[i];
if(nrow>=0 && nrow<n && ncol>=0 && ncol<m && image[nrow][ncol]==iniColor && ans[nrow][ncol]!=newcolor)
dfs(nrow,ncol,ans,image,newcolor,delrow,delcol,iniColor);
}
}
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {

int iniColor= image[sr][sc];
vector<vector<int>> ans = image;
int delrow[]={-1,0,+1,0};
int delcol[]={0,+1,0,-1};
dfs(sr,sc,ans,image,color,delrow,delcol, iniColor);
return ans;
}
};

0 comments on commit 0a8c17c

Please sign in to comment.