From f4b2b68c0cd597dc98a82963c6c7d293613d396a Mon Sep 17 00:00:00 2001 From: anish602 <116447899+anish602@users.noreply.github.com> Date: Sat, 22 Oct 2022 21:45:26 +0530 Subject: [PATCH] Created Flood fill Added a flood fill based on the concept of graph. Kindly review it and merge it. --- Graphs/Flood Fill | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Graphs/Flood Fill 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); + } + } +};