BFS,可是我没有想到
class Solution {
public:
int openLock(vector<string>& deadends, string target) {
set<string>deadlocks(deadends.begin(),deadends.end());
if(deadlocks.count(target) || deadlocks.count("0000")) return -1;
queue<string>q;
q.push("0000");
set<string>vis;
vis.insert("0000");
int res = 0;
int dx[2] = {-1,1};
while(!q.empty())
{
int size = q.size();
while((size--)>0)
{
auto t = q.front();
q.pop();
if(t == target) return res;
for(int i = 0;i<t.size();i++)
{
for(int j = 0;j<2;j++)
{
string newt = t;
newt[i] = ((newt[i]-'0')+10+ dx[j])%10 + '0';
if(!vis.count(newt) && !deadlocks.count(newt))
{
vis.insert(newt);
q.push(newt);
}
}
}
}
res++;
}
return -1;
}
};