Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created May 21, 2018 14:56
Show Gist options
  • Save s4553711/81741e3e5516f1e7a450b0487190c50d to your computer and use it in GitHub Desktop.
Save s4553711/81741e3e5516f1e7a450b0487190c50d to your computer and use it in GitHub Desktop.
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) return image;
int m = image.size();
int n = image[0].size();
floodFill(image, sc, sr, n, m, image[sr][sc], newColor);
return image;
}
void floodFill(vector<vector<int>>& image,
int x, int y, int n, int m, int orgColor, int newColor) {
if (x < 0 || x >= n || y < 0 || y >= m) return;
if (image[y][x] != orgColor) return;
image[y][x] = newColor;
floodFill(image, x + 1, y, n, m, orgColor, newColor);
floodFill(image, x - 1, y, n, m, orgColor, newColor);
floodFill(image, x, y + 1, n, m, orgColor, newColor);
floodFill(image, x, y - 1, n, m, orgColor, newColor);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment