-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathLC0733.cpp
executable file
·45 lines (40 loc) · 1.02 KB
/
LC0733.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
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Problem Statement: https://leetcode.com/problems/flood-fill/
Time: O(m • n)
Space: O(m • n)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
vector<vector<int>> floodFill(vector<vector<int>> image, int sr, int sc, int newColor) {
int m, n, color;
queue<pair<int, int>> q;
// initialization
m = image.size();
n = image[0].size();
color = exchange(image[sr][sc], newColor);
vector<int> x_dir = {-1, 0, 1, 0};
vector<int> y_dir = {0, -1, 0, 1};
if (color != newColor)
q.push({sr, sc});
// helper function
auto valid = [&](int x, int y) -> bool {
return (x >= 0 && x < m) && (y >= 0 && y < n);
};
// flood-fill algorithm
while (!q.empty()) {
tie(sr, sc) = q.front();
q.pop();
for (int i = 0; i < x_dir.size(); i++) {
int new_x, new_y;
new_x = sr + x_dir[i];
new_y = sc + y_dir[i];
if (valid(new_x, new_y) && image[new_x][new_y] == color) {
image[new_x][new_y] = newColor;
q.push({new_x, new_y});
}
}
}
return image;
}
};