Skip to content

Commit

Permalink
Solution to #11
Browse files Browse the repository at this point in the history
  • Loading branch information
bharatr21 committed May 11, 2020
1 parent b59da30 commit c160cb4
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Solutions/S11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//Utility to actually flood fill the 2D matrix
void floodFillUtil(vector<vector<int>>& 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<vector<int>> floodFill(vector<vector<int>>& 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;
}

0 comments on commit c160cb4

Please sign in to comment.