diff --git a/Graphs/Flood Fill b/Graphs/Flood Fill new file mode 100644 index 0000000..541c371 --- /dev/null +++ b/Graphs/Flood Fill @@ -0,0 +1,21 @@ +class Solution { +public: + +vector> floodFill(vector>& image, int sr, int sc, int newColor) { + int oldColor = image[sr][sc]; + dfs(image ,sr , sc , oldColor , newColor); + return image; + } + + void dfs(vector>& 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); + } + } +};