Catch That Cow

典型的模版题,很多方法可以解决,没什么难点,直接放代码了

#include
#include
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 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 型的队列

你可能感兴趣的:(Catch That Cow)