LeetCode 733
Flood Fill
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
int temp = image[sr][sc];
if (temp == newColor) return image;
Flood(image, sr, sc, newColor, temp);
return image;
}
void Flood(vector<vector<int>>& image, int sr, int sc, int newColor, int temp) {
if (sr >= 0 && sr < image.size() && sc >= 0 && sc < image[0].size() && image[sr][sc] == temp) {
image[sr][sc] = newColor;
Flood(image, sr-1, sc, newColor, temp);
Flood(image, sr+1, sc, newColor, temp);
Flood(image, sr, sc-1, newColor, temp);
Flood(image, sr, sc+1, newColor, temp);
}
return;
}
};