B - Catch That Cow

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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.
 
//用bfs,从当前位置开始,不断的试探,+1,-1,*2。直到找到牛,农民位置大于牛的时候,直接输出农民位置减牛的位置就行。
 
 #include <stdio.h>
#include <string.h>
#include <queue>
using namespace std; const int N = ;
int map[N+];
int n,k; struct node
{
int x,step;
}; int check(int x)
{
if(x< || x>=N || map[x])
return ;
return ;
} int bfs(int x)
{
queue <node> Q;
node a,next; a.x = x;
a.step = ;
map[x] = ;
Q.push(a); while(!Q.empty())
{
a = Q.front();
Q.pop(); if(a.x == k)
return a.step;
next = a;
//每次都将三种状况加入队列之中
next.x = a.x+;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
next.x = a.x-;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
next.x = a.x*;
if(check(next.x))
{
next.step = a.step+;
map[next.x] = ;
Q.push(next);
}
}
return ;
} int main()
{
int ans;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(map,,sizeof(map));
if (n>k) ans=n-k;
else ans = bfs(n);
printf("%d\n",ans);
}
return ;
}

 

B - Catch That Cow (抓牛)的更多相关文章

  1. BZOJ1646: [Usaco2007 Open]Catch That Cow 抓住那只牛

    1646: [Usaco2007 Open]Catch That Cow 抓住那只牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 634  Solved ...

  2. BZOJ 1646: [Usaco2007 Open]Catch That Cow 抓住那只牛( BFS )

    BFS... -------------------------------------------------------------------------------------------- ...

  3. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  4. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  5. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  6. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  7. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

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

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

  9. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

随机推荐

  1. php 基础入门篇之前言

    本人文笔不好,言辞简陋.没有华丽的语句,也没有精致的词语.仅仅是想给大家说一下,和大家一块学习,共同进步.学一点技术,找个吃饭的手段!接下来我会梳理一下知识,算是温故知新,同一时候也算是和大家一起在学 ...

  2. [Unity3D]查看与设置游戏帧数FPS

    原地址:http://blog.sina.com.cn/s/blog_5b6cb9500101bta4.html 关于FPS,在PC端来说,游戏帧数跑得越高越好,FPS跑得越高游戏就越流畅,当然太高也 ...

  3. spring-boot-redis-cluster简单整合例子

    代码地址如下:http://www.demodashi.com/demo/13184.html 一.前言 spring-boot项目整合redis很常见,Redis 一般上生产的时候都是以集群模式部署 ...

  4. 神奇的canvas——点与线绘制的绚丽动画效果

    代码地址如下:http://www.demodashi.com/demo/11636.html 前言 之前在某网站上看到了一个canvas绘制的动画效果,虽然组成的元素很简单,只有点和线,但是视觉效果 ...

  5. prototype 用法

    prototype使得js面向对象使用了prototype之后,使用它里面的属性或者函数 需要new出一个对象才可以使用.否则不使用prototype,直接向对象注入 function Person( ...

  6. MATLAB 的日期和时间

    MATLAB的日期和时间常用函数 函数 说明 calender 返回日历 clock 当前时间 date 当前日期 weekday 星期几 now 当前的日期和时间 datevec 以向量显示日期 d ...

  7. 02-3设置第一启动项--进入BIOS设置USB方式启动

    设置USB方式启动 https://zhinan.sogou.com/guide/detail/?id=1610014869 如何设置电脑从U盘启动呢?今天小编教大家如何进入BIOS设置USB方式启动 ...

  8. Audio简介

    本片只简单从硬件角度简介Audio AC97/HDA Audio总线分两种: (1)I2S (2)HDA HD Audio spec Audio verb table是用来初始化audio的,一个au ...

  9. typeof 和 Object.prototype.toString.call 数据类型判断的区别

    使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种. 但 Object.prototype ...

  10. Sql语句查询XML - 小结

    --两种方式查询 DECLARE @varXML XML, @varXML1 XML --.xml数据源为属性方式 SET @varXML = '<PARAM> <Row FID = ...