From c160cb4933638590152c697e6d1331a0b53e7079 Mon Sep 17 00:00:00 2001 From: Bharat123rox Date: Mon, 11 May 2020 14:23:34 +0530 Subject: [PATCH] Solution to #11 --- Solutions/S11.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Solutions/S11.cpp diff --git a/Solutions/S11.cpp b/Solutions/S11.cpp new file mode 100644 index 0000000..cb230fe --- /dev/null +++ b/Solutions/S11.cpp @@ -0,0 +1,19 @@ +//Utility to actually flood fill the 2D matrix +void floodFillUtil(vector>& image, int sr, int sc, int m, int n, int prevColor, int newColor) { + if(sr < 0 || sr >= m || sc < 0 || sc >= n) return; + else if(image[sr][sc] == newColor || image[sr][sc] != prevColor) return; + image[sr][sc] = newColor; + floodFillUtil(image, sr - 1, sc, m, n, prevColor, newColor); + floodFillUtil(image, sr + 1, sc, m, n, prevColor, newColor); + floodFillUtil(image, sr, sc - 1, m, n, prevColor, newColor); + floodFillUtil(image, sr, sc + 1, m, n, prevColor, newColor); +} + +//Wrapper function +vector> floodFill(vector>& image, int sr, int sc, int newColor) { + int prevColor = image[sr][sc]; + int m = image.size(); + int n = image[0].size(); + floodFillUtil(image, sr, sc, m, n, prevColor, newColor); + return image; +} \ No newline at end of file