generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
53 lines (46 loc) · 1.43 KB
/
solution.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
#include <cstdint>
#include <string>
#include <vector>
class Solution {
public:
int openLock(std::vector<std::string>& deadends, std::string targetStr) {
if (targetStr == "0000") return 0;
std::vector<bool> seen(10000, false);
for (const auto& deadend : deadends) {
seen[std::stoi(deadend)] = true;
}
if (seen[0]) return -1;
std::vector<std::int16_t> queue(1500);
std::size_t queueBegin{0};
std::size_t queueEnd{1};
queue[queueBegin] = 0;
seen[0] = true;
int steps{1};
int target{std::stoi(targetStr)};
while (queueBegin != queueEnd) {
for (std::size_t i{(queueBegin < queueEnd ? queueEnd : 1500 + queueEnd) - queueBegin}; i > 0; --i) {
for (std::int16_t exp{1}; exp <= 1000; exp *= 10) {
auto key{queue[queueBegin]};
key += (key / exp) % 10 == 9 ? -9 * exp : exp;
if (key == target) return steps;
if (!seen[key]) {
queue[queueEnd] = key;
if (++queueEnd == 1500) queueEnd = 0;
seen[key] = true;
}
key = queue[queueBegin];
key += (key / exp) % 10 == 0 ? 9 * exp : -exp;
if (key == target) return steps;
if (!seen[key]) {
queue[queueEnd] = key;
if (++queueEnd == 1500) queueEnd = 0;
seen[key] = true;
}
}
if (++queueBegin == 1500) queueBegin = 0;
}
++steps;
}
return -1;
}
};