-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0752_Open-the-Lock.cpp
More file actions
56 lines (47 loc) · 1.36 KB
/
0752_Open-the-Lock.cpp
File metadata and controls
56 lines (47 loc) · 1.36 KB
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
class Solution {
public:
struct si {
string s;
int steps;
};
string rotate(string s, int i, int dir) {
s[i] = (((s[i] - '0') + dir + 10) % 10) + '0';
return s;
}
int openLock(vector<string>& deadends, string target) {
// bfs with graph, which consist of 10000 nodes.
// each node has 8 neighbors.
queue<si> q;
vector<int> dirs = {-1, 1};
unordered_set<string> dead;
for (auto &s : deadends) {
dead.insert(s);
}
if (dead.count("0000") <= 0)
q.push(si(
"0000",
0
));
unordered_set<string> found;
found.insert("0000");
bool is_valid = false;
int ret = std::numeric_limits<int>::max();
while (!q.empty()) {
auto top = q.front();
q.pop();
if (top.s == target) {
return top.steps;
}
for (int i = 0; i < 4; ++i) {
for (auto dir : dirs) {
auto new_str = rotate(top.s, i, dir);
if (dead.count(new_str) <= 0 && found.count(new_str) <= 0) {
q.push(si(new_str, top.steps+1));
found.insert(new_str);
}
}
}
}
return -1;
}
};