forked from shuboc/LeetCode-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
open-the-lock.cpp
40 lines (39 loc) · 1.32 KB
/
open-the-lock.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
// Time: O(k * n^k + d), n is the number of alphabets,
// k is the length of target,
// d is the size of deadends
// Space: O(k * n^k + d)
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
unordered_set<string> dead(deadends.begin(), deadends.end());
vector<string> q{"0000"};
unordered_set<string> lookup{"0000"};
int depth = 0;
while (!q.empty()) {
vector<string> next_q;
for (const auto& node : q) {
if (node == target) {
return depth;
}
if (dead.count(node)) {
continue;
}
for (int i = 0; i < 4; ++i) {
auto n = node[i] - '0';
for (const auto& d : {-1, 1}) {
auto nn = (n + d + 10) % 10;
auto neighbor = node;
neighbor[i] = '0' + nn;
if (!lookup.count(neighbor)) {
lookup.emplace(neighbor);
next_q.emplace_back(neighbor);
}
}
}
}
swap(q, next_q);
++depth;
}
return -1;
}
};