forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
coloring-a-border.cpp
52 lines (47 loc) · 1.71 KB
/
coloring-a-border.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
46
47
48
49
50
51
52
// Time: O(m * n)
// Space: O(m + n)
class Solution {
public:
template <typename T>
struct PairHash {
size_t operator()(const pair<T, T>& p) const {
size_t seed = 0;
seed ^= std::hash<T>{}(p.first) + 0x9e3779b9 + (seed<<6) + (seed>>2);
seed ^= std::hash<T>{}(p.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
return seed;
}
};
vector<vector<int>> colorBorder(vector<vector<int>>& grid, int r0, int c0, int color) {
static const vector<pair<int, int>> directions{{0, -1}, {0, 1},
{-1, 0}, {1, 0}};
vector<pair<int, int>> borders;
unordered_set<pair<int, int>, PairHash<int>> lookup{make_pair(r0, c0)};
queue<pair<int, int>> q({make_pair(r0, c0)});
while (!q.empty()) {
int r, c;
tie(r, c) = q.front(); q.pop();
bool is_border = false;
for (const auto& dir : directions) {
const auto nr = r + dir.first, nc = c + dir.second;
if (!((0 <= nr && nr < grid.size()) &&
(0 <= nc && nc < grid[0].size()) &&
grid[nr][nc] == grid[r][c])) {
is_border = true;
continue;
}
if (lookup.count(make_pair(nr, nc))) {
continue;
}
lookup.emplace(nr, nc);
q.emplace(nr, nc);
}
if (is_border) {
borders.emplace_back(r, c);
}
}
for (const auto& border : borders) {
grid[border.first][border.second] = color;
}
return grid;
}
};