map<ll, int> ans;
int f1(ll x)
{
if (ans[x]) return ans[x];
queue<ll> qll;
qll.push(1);
while (!qll.empty()){
ll t = qll.front(); qll.pop();
if (t < 1 || t > 1e18) continue;
if (t == x) return ans[t];
if (ans[t + 1] == 0){
ans[t + 1] = ans[t] + 1;
qll.push(t + 1);
}
if (ans[t - 1] == 0){
ans[t - 1] = ans[t] + 1;
qll.push(t - 1);
}
if (ans[2 * t] == 0){
ans[2 * t] = ans[t] + 1;
qll.push(2 * t);
}
}
return 0;
}
[搜索]bfs求最小变化次数
最新推荐文章于 2021-03-16 15:42:17 发布