https://leetcode.com/problems/open-the-lock/description/
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
set<string>deadlock(deadends.begin(), deadends.end());
if(deadlock.count("0000")) return -1;
int res = 0;
set<string> visited{{"0000"}};
queue<string>Q{{"0000"}};
while (!Q.empty()) {
res += 1;
for(int k=Q.size(); k>0; k--){
auto t = Q.front(); Q.pop();
for(int i=0; i<t.size(); i++){
for(int j=-1; j<=1; j++){
if(j==0) continue;
string str = t;
str[i] = ((t[i]-'0')+10+j)%10 + '0';
if(str==target) return res;
if(!visited.count(str)&&!deadlock.count(str)) Q.push(str);
visited.insert(str);
}
}
}
}
return -1;
}
};