Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10166    Accepted Submission(s): 3179

Problem 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 X - 1 or X + 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.

 #include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int b,e;
int Mintim;
struct node
{
int pos,t;
}k,tem;
int vis[+];
queue<node> s;
void bfs()
{
while(!s.empty())
s.pop();
k.pos=b,k.t=;
s.push(k);
while(!s.empty())
{
k=s.front();
s.pop();
if(k.pos==e)
{
Mintim=k.t;
return;
}
if(k.pos<||k.pos>||vis[k.pos]) continue;
vis[k.pos]=;
tem.t=k.t+;
tem.pos=k.pos+;
s.push(tem);
tem.pos=k.pos-;
s.push(tem);
tem.pos=k.pos*;
s.push(tem);
}
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&b,&e)!=EOF)
{
memset(vis,,sizeof(vis));
bfs();
printf("%d\n",Mintim);
}
return ;
}
 
 

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

  1. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  2. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  3. HDU 2717 Catch That Cow(BFS)

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  4. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

  5. ***参考Catch That Cow(BFS)

    Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  6. Catch That Cow (bfs)

    Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...

  7. poj 3278(hdu 2717) Catch That Cow(bfs)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  8. 题解报告:hdu 2717 Catch That Cow(bfs)

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  9. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 #include<s ...

随机推荐

  1. JSON C# Class Generator是一个从JSON文本中生成C#内的应用程序

    JSON C# Class Generator是一个从JSON文本中生成C#内的应用程序 .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator   ...

  2. 配置Kestrel 网址Urls

    配置Kestrel 网址Urls ASP.NET Core中如何配置Kestrel Urls呢,大家可能都知道使用UseUrls() 方法来配置. 今天给介绍全面的ASP.NET Core 配置 Ur ...

  3. 利用Azure高级存储搭建高性能Linux服务器(2)

    我们首先来测试随机写的IOPS,可以看到随机写的IOPS可以达到,顺序写的IOPS可以达到: $ sudo fio -filename=/data/testfile -direct=1 -iodept ...

  4. 获取url 参数

    epresssjs 里面请求参数,4.x 里面有3种方法 1.req.params app.get('user/:id',function(req,res){ res.send('user',req. ...

  5. oracle安装报错2

    [oracle@centos1 database]$ ./runInstaller Starting Oracle Universal Installer... Checking installer ...

  6. 必须得是一万小时的 刻意训练(deliberate practice)

    成功素质1:一万小时与格物致知 “格物致知14”的概念,我是从张银奎11老师那里了解到的.它的意思是“推究事物的原理,从而获得知识”,跟我在<透过现象看本质 - 写在观看WWDC 2016 Ke ...

  7. 清除IE输入框眼睛和叉叉

    /* 清除IE输入框眼睛和叉叉 */::-ms-clear { display: none; } ::-ms-reveal { display: none; }

  8. 插入排序(Insertion Sort)

    这是排序算法中最常见的排序方法,也是初学者使用最多的.有时候我们在生活中也会不自觉地用到插入排序,例如: 给手里的牌排序 这是最常见的例子之一,我们通常从纸牌的一边开始看,找到一张位置不正确的,把它拿 ...

  9. OpenWrt openssl library

    Please install the openssl library (with development headers) sudo apt-get install libssl; rite fail ...

  10. 2016"百度之星" - 资格赛(Astar Round1) 1004

    思路:题目很简单,直接用map记录每个字符串的个数就可以了.记得对每个字符串先sort(). AC代码: #include <cstdio> #include <stdlib.h&g ...