标签:push pac space else mes continue pop nod cat
典型的模版题,很多方法可以解决,没什么难点,直接放代码了
#include <iostream>
#include <queue>
using namespace std;
int n, k;
bool look[100001];
struct node {
int n, step;
node(int x=0, int y = 0) :n(x), step(y) {}
};
int bfs() {
queue<node> que;
que.push(node(n));
int temp;
node now;
while (!que.empty()) {
now = que.front();
que.pop();
if (now.n == k)break;
if (now.n <= 100000 && now.n >= 0) {
if (look[now.n])continue;
else look[now.n] = 1;
temp = now.step + 1;
que.push(node((now.n - 1),temp));
que.push(node((now.n + 1), temp));
que.push(node((now.n << 1), temp));
}
}
return now.step;
}
int main() {
cin >> n >> k;
int ans = bfs();
cout << ans;
return 0;
}
//也可以用int look数组记录步数,变成int 型的队列
标签:push pac space else mes continue pop nod cat
原文地址:https://www.cnblogs.com/sos3210/p/12253642.html