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

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.

Source


BFS
小优化:k<n直接n-k
注意边界处理,x<=k而不是x*2<=k,因为有时候还是要先两杯再往回走,比如n很靠前
//
// main.cpp
// poj3278
//
// Created by Candy on 9/27/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=2e5+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,k,head=,tail=,ans=INF;
struct data{
int x,d;
data(int a=,int b=):x(a),d(b){}
}q[N];
int vis[N];
void bfs(){
q[++tail]=data(n,);vis[n]=;
while(head<=tail){
data now=q[head++];
int x=now.x,d=now.d;
if(x==k){printf("%d",d);return;}
if(x+<=k&&!vis[x+]){
vis[x+]=;
q[++tail]=data(x+,d+);
}
if(x->=&&!vis[x-]){
vis[x-]=;
q[++tail]=data(x-,d+);
}
if(x<=k&&!vis[x<<]){
vis[x<<]=;
q[++tail]=data(x<<,d+);
}
}
}
int main(int argc, const char * argv[]) {
n=read();k=read();
if(k<n)printf("%d",n-k);
else bfs();
return ;
}

POJ3083Catch 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)

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

  3. POJ3278 Catch That Cow(BFS)

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

  4. poj3278Catch That Cow(BFS)

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

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

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

  6. 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 ...

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

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

  8. Catch That Cow (BFS广搜)

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

  9. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

随机推荐

  1. JS中数组去除重复的方法

    function unique(arr) { var result = [], hash = []; for (var i = 0, elem; (elem = arr[i]) != null; i+ ...

  2. Electron笔记

    一个能让你用Web技术开发桌面应用的开源项目.这里做一个笔记(非正式文章): 官网地址:http://electron.atom.io/ API相关 Electron提供的主进程接口.渲染进程接口.共 ...

  3. ArcGIS Desktop 遇到严重的应用程序错误

    由于项目初验,忙了几个月(感觉忙得并不值),好久都没更新博客了. 一.问题 在关闭ArcMap时,ArcGIS Desktop 遇到严重的应用程序错误.环境是Windows 10,新装的系统.以前出现 ...

  4. 系统补丁对sharepoint很重要

    系统补丁对sharepoint很重要,会提高sharepoint运行效率,加载速度明显变快.

  5. 自定义有监听器的ScrollView

    public class ObservableScrollView extends ScrollView { public ObservableScrollView(Context context) ...

  6. 【代码笔记】iOS-UIScrollerView里有两个tableView

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  7. FMDB简单用法

    首先引入类库 声明数据库和路径 /**  声明数据库对象  */ @property (nonatomic, strong) FMDatabase *dataBase; /**  声明存储路径  */ ...

  8. IDEA中,将文件夹加入classpath

    加载文件时遇到某些文件加载不到,可能是因为该文件不在classpath中. 加入方法: 1.File - Project Structure 2.如图 modules - 选择需要加入的文件夹, 点击 ...

  9. new ActiveXObject("Scripting.FileSystemObject") 时抛出异常 .

    使用JScript读写本地文件时,会使用Scripting.FileSystemObject控件. IE默认是不允许运行这类“未标记为安全执行脚本的ActiveX控件”的. 因此执行下行代码时: fs ...

  10. C#中的Lambda表达式和表达式树

    在C# 2.0中,通过方法组转换和匿名方法,使委托的实现得到了极大的简化.但是,匿名方法仍然有些臃肿,而且当代码中充满了匿名方法的时候,可读性可能就会受到影响.C# 3.0中出现的Lambda表达式在 ...