-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path733.cpp
29 lines (28 loc) · 845 Bytes
/
733.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
// bfs.cpp
class Solution {
const int dirx[4] = {0, 1, 0, -1};
const int diry[4] = {1, 0, -1, 0};
public:
vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc,
int newColor) {
deque<pair<int, int>> dq;
dq.push_back(make_pair(sr, sc));
int color = image[sr][sc];
if (color == newColor)
return image;
image[sr][sc] = newColor;
int row = image.size(), col = image[0].size();
while (!dq.empty()) {
auto point = dq.front();
dq.pop_front();
for (int i = 0; i < 4; ++i) {
int x = point.first + dirx[i], y = point.second + diry[i];
if (x >= 0 && x < row && y >= 0 && y < col && image[x][y] == color) {
image[x][y] = newColor;
dq.push_back(make_pair(x, y));
}
}
}
return image;
}
};