-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path752. Open the Lock
40 lines (36 loc) · 1.22 KB
/
752. Open the Lock
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
class Solution
{
public:
int openLock(vector<string>& deadends, string target)
{
int operations = 0;
unordered_set<string> dead, vis;
for(auto &deadend : deadends) dead.insert(deadend);
if(dead.count("0000")) return -1;
queue<string> q;
q.push("0000");
vis.insert("0000");
while(q.size())
{
int sz = q.size();
while(sz--)
{
string cur_state = q.front();q.pop();
if(cur_state == target) return operations;
for(int i = 0; i < 4; i++)
{
string new_state = cur_state;
new_state[i] == '9' ? new_state[i] = '0' : new_state[i]++;
if(!vis.count(new_state) and !dead.count(new_state))
q.push(new_state), vis.insert(new_state);
new_state = cur_state;
new_state[i] == '0' ? new_state[i] = '9' : new_state[i]--;
if(!vis.count(new_state) and !dead.count(new_state))
q.push(new_state), vis.insert(new_state);
}
}
operations++;
}
return -1;
}
};