-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path752.cpp
More file actions
48 lines (44 loc) · 1.39 KB
/
752.cpp
File metadata and controls
48 lines (44 loc) · 1.39 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
// Daily 22.4.2024
// 752. Open the Lock
// https://leetcode.com/problems/open-the-lock/submissions/1238868785/
// Could be further optimized by using shorts in the queue via stoi()
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
bitset<10000> ends;
/// Mark dead ends as already visited
for(auto s : deadends){
ends[stoi(s)] = true;
}
if(ends[0]) return -1;
int result = 0;
// BFS
queue<string> q{{"0000"}};
while(!q.empty()){
int size = q.size();
while(size--){
const string cur = q.front();
q.pop();
if(cur == target) return result;
for(int i = 0; i <4; i++){
string inc = cur;
string dec = cur;
inc[i] = (cur[i] == '9' ? '0' : cur[i]+1);
dec[i] = (cur[i] == '0' ? '9' : cur[i]-1);
short u = stoi(inc);
short d = stoi(dec);
if(!ends[u]){
q.push(inc);
ends[u] = true;
}
if(!ends[d]){
q.push(dec);
ends[d] = true;
}
}
}
result++;
}
return -1;
}
};