Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 37094   Accepted: 11466

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

* Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
* Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

Input

Line 1: Two space-separated integers: 
N and 
K

Output

Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

Sample Input

5 17

Sample Output

4

Hint

The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
刚做的时候发生了很多奇异YY, 搞的我不知道重写了多少遍。。

从当前位置local,向local+1, local-1, local*2这三个方向扩展。
code:
#include <queue>
#include <cstdio>
#include<cstring>
#define N 100001
using namespace std;
int n, k, ans;
bool vis[N];
struct node {
int local, time;
};
void bfs() {
int t, i;
node now, tmp;
queue<node> q;
now.local = n;
now.time = 0;
q.push(now);
memset(vis,false,sizeof(vis));
vis[now.local] = true;
while(!q.empty()) {
now = q.front();
q.pop();
for(i=0; i<3; i++) {
if(0==i) t = now.local-1;
else if(1==i) t = now.local+1;
else if(2==i) t = now.local*2;
if(t<0||t>N||vis[t]) continue;
if(t==k) {
ans = now.time+1;
return;
}
vis[t] = true;
tmp.local = t;
tmp.time = now.time+1;
q.push(tmp);
}
}
} int main() {
while(~scanf("%d%d",&n,&k)) {
if(n>=k) printf("%d\n",n-k);
else {
bfs();
printf("%d\n",ans);
}
}
}

poj3278Catch That Cow(BFS)的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  3. POJ3083Catch That Cow[BFS]

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77420   Accepted: 24457 ...

  4. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  5. POJ3278 Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  6. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  7. poj 3278 Catch That Cow bfs

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. Catch That Cow (BFS广搜)

    问题描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

随机推荐

  1. cocos2d-x游戏开发系列教程-超级玛丽08-消息机制

    在超级玛丽游戏里,地图类CMGameMap负责所有的程序逻辑,它包含了背景地图,包含了游戏元素精灵,当游戏中的精灵之间发生碰撞时,比如马里奥撞上砖头这种事情发生时,马里奥对象本身不知道怎么处理这个逻辑 ...

  2. vc 国际化的资源文件处理

    MS Windows操作系统是一个世界上广泛使用的操作系统,对于不同语种的国家MS Windows有相应语种的版本.在不同语种的Windows平台上应该运行相应语种的应用程序.也就是说程序的用户界面( ...

  3. JSON XML IO数据操作

    一.XML解析 通过继承org.xml.sax.helpers.DefaultHandler类,覆写characters(),startDocument(),startElement(),endEle ...

  4. sqlite3 脚本的使用

    本来想找个专门查看数据库的工具的,后来发现脚本也非常easy,就直接用脚本了. sqlite3的脚本能够方便的查看当前数据库里的内容,使用起来事实上非常easy,这一列举一些最简单的使用方法,以后假设 ...

  5. Android FragmentPagerAdapter和FragmentStatePagerAdapter的区别

    FragmentPagerAdapter官方解释: This version of the pager is best for use when there are a handful of typi ...

  6. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

  7. Python-zip压缩-解压

    #打包成zip文件 import zipfile f = zipfile.ZipFile('archive.zip','w',zipfile.ZIP_DEFLATED) f.write('file_t ...

  8. IOS7修改Navigation Bar上的返回按钮文本颜色,箭头颜色以及导航栏按钮的颜色

    解决方法 1: 自从IOS7后UINavigationBar的一些属性的行为发生了变化.你可以在下图看到: 现在,如果你要修改它们的颜色,用下面的代码: 1 2 3 4 self.navigation ...

  9. 基于FPGA的DW8051移植(二)

    基于上一篇博文继续,本来想换到oc8051,但是还是不甘心,弄了这么久还是没有弄出来,真是打击屎了. 上一篇说3f进入了operation code所以判断是代码错误,后来发现不可以这么判断. 因为地 ...

  10. QDebug &operator<<出错(根据QString来找,是不得要领的,而是应该根据QString所在的对象来思考)

    程序运行后,总是崩溃在这个地方:inline QDebug &operator<<(const QString & t) 我应该用什么办法找出是哪个QString出了问题呢 ...