forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest-bridge.cpp
85 lines (80 loc) · 3.19 KB
/
shortest-bridge.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Time: O(n^2)
// Space: O(n^2)
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;
}
};
int shortestBridge(vector<vector<int>>& A) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
unordered_set<pair<int, int>, PairHash<int>> lookup;
unordered_set<pair<int, int>, PairHash<int>> target;
auto islands = get_islands(A);
lookup = move(islands[0]);
target = move(islands[1]);
queue<pair<pair<int, int>, int>> q;
for (const auto& node : lookup) {
q.emplace(node, 0);
}
while (!q.empty()) {
pair<int, int> node;
int dis;
tie(node, dis) = q.front(); q.pop();
if (target.count(node)) {
return dis - 1;
}
for (const auto& d : directions) {
pair<int, int> nei = make_pair(node.first + d.first, node.second + d.second);
if (0 > nei.first || nei.first >= A.size() ||
0 > nei.second || nei.second >= A[0].size() ||
lookup.count(nei)) {
continue;
}
q.emplace(nei, dis + 1);
lookup.emplace(nei);
}
}
}
private:
vector<unordered_set<pair<int, int>, PairHash<int>>> get_islands(const vector<vector<int>>& A) {
static const vector<pair<int, int>> directions{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<unordered_set<pair<int, int>, PairHash<int>>> islands;
unordered_set<pair<int, int>, PairHash<int>> done;
for (int r = 0; r < A.size(); ++r) {
for (int c = 0; c < A[0].size(); ++c) {
if (A[r][c] == 0 || done.count(make_pair(r, c))) {
continue;
}
vector<pair<int, int>> s{{r, c}};
unordered_set<pair<int, int>, PairHash<int>> lookup(s.cbegin(), s.cend());
while (!s.empty()) {
auto node = s.back(); s.pop_back();
for (const auto& d : directions) {
pair<int, int> nei = make_pair(node.first + d.first, node.second + d.second);
if (0 > nei.first || nei.first >= A.size() ||
0 > nei.second || nei.second >= A[0].size() ||
lookup.count(nei) || A[nei.first][nei.second] == 0) {
continue;
}
s.emplace_back(nei);
lookup.emplace(nei);
}
}
for (const auto& node : lookup) {
done.emplace(node);
}
islands.emplace_back(move(lookup));
if (islands.size() == 2) {
break;
}
}
}
return islands;
}
};