-
Notifications
You must be signed in to change notification settings - Fork 9
/
FloodFill.cpp
32 lines (29 loc) · 1.47 KB
/
FloodFill.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Problem: https://leetcode.com/problems/flood-fill/
#include <unordered_set>
#include <vector>
class FloodFill {
public:
vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
unordered_set<string> seen;
vector<vector<int>> updated = image;
int max_rows = image.size();
int max_cols = image[0].size();
int oldColor = image[sr][sc];
floodfill(image, updated, seen, sr, sc, oldColor, newColor, max_rows, max_cols);
return updated;
}
void floodfill(const vector<vector<int>>& image, vector<vector<int>>& updated,
unordered_set<string>& seen, int r, int c,
int oldColor, int newColor, int max_rows, int max_cols) {
if (r < 0 || c < 0 || r >= max_rows || c >= max_cols) return;
string pos = to_string(r) + ":" + to_string(c);
if (seen.find(pos) != seen.end()) return;
seen.insert(pos);
if (image[r][c] != oldColor) return;
floodfill(image, updated, seen, r + 1, c, oldColor, newColor, max_rows, max_cols);
floodfill(image, updated, seen, r, c + 1, oldColor, newColor, max_rows, max_cols);
floodfill(image, updated, seen, r - 1, c, oldColor, newColor, max_rows, max_cols);
floodfill(image, updated, seen, r, c - 1, oldColor, newColor, max_rows, max_cols);
if (updated[r][c] == oldColor) updated[r][c] = newColor;
}
};