Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Graphs/Flood Fill
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:

vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int oldColor = image[sr][sc];
dfs(image ,sr , sc , oldColor , newColor);
return image;
}

void dfs(vector<vector<int>>& image,int sr, int sc, int oldColor, int newColor)
{
if(sr < image.size() && sc < image[0].size() && image[sr][sc] == oldColor && image[sr][sc] != newColor)
{
image[sr][sc] = newColor;
dfs(image , sr+1 , sc , oldColor , newColor);
dfs(image , sr-1 , sc , oldColor , newColor);
dfs(image , sr , sc+1 , oldColor , newColor);
dfs(image , sr , sc-1 , oldColor , newColor);
}
}
};