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. HBase源代码分析之MemStore的flush发起时机、推断条件等详情(二)

    在<HBase源代码分析之MemStore的flush发起时机.推断条件等详情>一文中,我们具体介绍了MemStore flush的发起时机.推断条件等详情.主要是两类操作.一是会引起Me ...

  2. vue 项目中 自定义 webpack 的 配置文件(webpack.config.babel.js)

    webpack.config.babel.js,这样命名是想让webpack在编译的时候自动识别es6的语法,现在貌似不需要这样命名了,之前用webpack1.x的时候貌似是需要的 let path ...

  3. iOS 物流信息时间轴

    代码地址如下:http://www.demodashi.com/demo/11958.html timelineLogistics 是模仿淘宝物流信息时间轴界面的自定义View 准备工作 引入Maso ...

  4. Docker的Jenkins Pipeline工作流

    原文地址:http://www.youruncloud.com/blog/127.html 分享主题 一个软件产品的开发周期中,尤其是敏捷开发,持续集成和持续部署是必不可少的环节,而随着产品的丰富,模 ...

  5. angular4 开发记录

    1,传值问题 page setValue:  [routerLink]="['/product-details', product.id]"> ts     seValue: ...

  6. Linux管理员必须知道的sudo命令

    "Sudo"是Unix/Linux平台上的一个很实用的工具,它同意系统管理员分配给普通用户一些合理的"权利",让他们执行一些仅仅有超级用户或其它 特许用户才干完 ...

  7. iOS10 获取系统通讯录新方法

    #import <ContactsUI/ContactsUI.h> 遵循代理 CNContactPickerDelegate 调用通讯录 如果在iOS10的机器上调用以前的ABPeople ...

  8. DataUml Design 教程2-实体建模

    DataUml Design 实体建模基于UML类图标准来设计,支持一对一.一对多.多对多关联.模型与开发语言和数据库绑定.1.新建数据模型    1).选择“数据模型” 节点,点击鼠标右键,在菜单中 ...

  9. LeetCode447. Number of Boomerangs

    Description Given n points in the plane that are all pairwise distinct, a "boomerang" is a ...

  10. GitBlit (1)-- 在linux 安装 GitBlit 并运行

    Git是一款注重速度.数据完整性.分布式支持和非线性工作流的分布式版本控制工具.Git最初由Linus Torvalds在2005年为Linux内核开发而设计,如今已经成为被广泛接受的版本控制系统. ...