码迷,mamicode.com
首页 > 其他好文 > 详细

Catch That Cow

时间:2020-02-02 21:53:40      阅读:72      评论:0      收藏:0      [点我收藏+]

标签: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 型的队列

Catch That Cow

标签:push   pac   space   else   mes   continue   pop   nod   cat   

原文地址:https://www.cnblogs.com/sos3210/p/12253642.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!