Catch That Cow

2020-02-02 16:00:31来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

Catch That Cow

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

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


原文链接:https://www.cnblogs.com/sos3210/p/12253642.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C++ 一篇搞懂多态的实现原理

下一篇:全排列2