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. JQuery基础学习总结

    JQuery基础学习总结 简单总结下JQuery: 一:事件 1.change事件 <!DOCTYPE html> <html lang="en"> < ...

  2. sqlplus 一次奇葩问题 HTTP proxy setting has incorrect value

    y@y:~$ sqlplus Error 46 initializing SQL*PlusHTTP proxy setting has incorrect valueSP2-1502: The HTT ...

  3. JAVA字符串转日期或日期转字符串

    文章中,用的API是SimpleDateFormat,它是属于java.text.SimpleDateFormat,所以请记得import进 来! 用法: SimpleDateFormat sdf = ...

  4. 安卓开发中ScrollView不能用RelativeLayout的解决方案

    RelativeLayout的意义: 布局各个部件的相对布局.使得界面空间合理利用. 一.ScrollView局限: 滑动的只能是linearlayout,甚至整个布局都不能有RelativeLayo ...

  5. logstash date插件介绍

    时间处理(Date) 之前章节已经提过, filters/date 插件可以用来转换你的日志记录中的时间字符串,变成 LogStash::Timestamp 对象,然后转存到 @timestamp 字 ...

  6. mysql导入到elasticsearch

    JDBC importer for Elasticsearch Java Database 连接(JDBC) 导入运行获取数据从JDBC 源 安装: 1.在下面的步骤 替换<version> ...

  7. Java反编译插件jad

    原文地址:http://www.cnblogs.com/JimLy-BUG/p/5405868.html 1.首先下载jar文件:net.sf.jadclipse_3.3.0.jar  下载   2. ...

  8. xmind教程

    xmind是什么东西我不多说.作为一个程序员,我通常用来编写一个文档.比如某个模块的设计或者流程图. 一开始我是以word画图的方式来用xmind的,即想要什么图形,就去插入里面找.结果碰了一鼻子灰, ...

  9. DirectX 初始化DirectX(第一方式)

      上一章我们学会了如何C++Win32项目中搭建DirectX开发环境, 那么下面来写代码初始化DirectX吧O(∩_∩)O~. 首先你创建一个Win32程序,点击运行你可以看见一个window窗 ...

  10. 集成支付宝SDK遇到的坑

    一.首先我先把集成过程说一下.小编想说的话:支付宝是我做支付中觉得坑最多的一个,各种编译不过,各种出问题. 废话不多说,进入主题:1.首先当前是下载官方SDK啦,当前你也可以通过cocopods进行导 ...