Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 78114   Accepted: 24667

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

 
 #include "iostream"
#include "fstream"
#include "sstream"
#include "cstdio"
#include "queue" using namespace std ;
const int maxN = 1e5 + 1e3 ;
const int Max_Limit = 1e5 ;
const int INF = ;
typedef long long QAQ ; queue < int > Q ;
bool vis[ maxN ] ;
int step[ maxN ] ;
int K , N ; bool Check ( int x_x ) { return x_x == K ? true : false ; } int BFS ( ) {
int temp ;
bool Get_Target = false ;
Q.push ( N ) ;
vis[ N ] = true ;
while ( !Q.empty ( ) ) {
int tmp = Q.front ( ) ;
Q.pop ( ) ;
for ( int i= ; i< ; ++i ) {
switch ( i ) {
case :{
temp = tmp - ;
break;
}
case :{
temp = tmp + ;
break;
}
case :{
temp = tmp * ;
break;
}
} if ( temp > Max_Limit || temp < ) continue ;
if ( !vis[ temp ] ) {
Q.push( temp ) ;
step[ temp ] = step[ tmp ] + ;
vis[ temp ] = true ;
if ( Check ( temp ) ) {
Get_Target = true ;
return step[ temp ] ;
}
}
}
}
if ( !Get_Target ) return - ;
} int main ( ) {
scanf ( "%d %d" , &N , &K ) ;
if ( N >= K ) {
printf ( "%d\n" , N - K ) ;
goto End ;
}
printf ( "%d\n" , BFS ( ) ) ;
End :
return ;
}

2016-10-19 21:59:12

POJ 3278 题解的更多相关文章

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

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

  2. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  3. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  4. catch that cow POJ 3278 搜索

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

  5. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

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

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

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

  8. POJ 3278 Catch That Cow(模板——BFS)

    题目链接:http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a f ...

  9. POJ - 3278

    题目链接:http://poj.org/problem?id=3278 ac代码: #include <iostream>#include <stdio.h>#include ...

随机推荐

  1. 第二轮冲刺-Runner站立会议05

    今天:将baseadapter的原理弄清楚了 明天:解决适配问题 困难:程序会停止运行

  2. 6Hibernate进阶----青软S2SH(笔记)

    关于关联关系的配置,用注解配置如下(这里引用的jar包是javax.persistence) // @ManyToOne(fetch=FetchType.LAZY) @ManyToOne(fetch= ...

  3. Linux下提示 bash: xxx command not found

    今天在虚拟机上安装了CentOS5.5,发现运行一些很正常的诸如:init,shutdown,fdisk 等命令时,悍然提示: bash: xxx command not found. 那么,首先就要 ...

  4. php.ini 安全配置

    (1) 打开php的安全模式 php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/ ...

  5. java基础知识(三)java关键字

    关键字是电脑语言事先定义的,是特别意义的标识符,又叫保留字.用来表示一种数据类型或程序的结构等,关键字不能用作变量名.类名.方法名或参数.java目前共有50个关键字,其中"const&qu ...

  6. BZOJ 1923: [Sdoi2010]外星千足虫

    Description 给出几个异或方程组求解,\(n \leqslant 2000\) Sol 高斯消元. 直接消元就行,遇到自由元就直接输出,同时记录一下用到的最高行数. 复杂度不科学就可以用 b ...

  7. 如何更高效地定制你的bootstrap

    bootstrap已经作为前端开发必不可少的框架之一,应用bootstrap使得我们对布局.样式的设定变得非常简单.但bootstrap提供的默认样式往往不能满足我们的需求,从而定制化bootstra ...

  8. Qt:postEvent 与 customEvent() 函数 进行异步通信; 以及参数的传递 // 防止界面卡死;;

    class ColorChangeEvent : public QCustomEvent { public: ColorChangeEvent( QColor color ) : QCustomEve ...

  9. ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)

    题意有点难读懂 //网络流-EK算法-ACM计算机工厂-构图重点 //Time:0Ms Memory:208K #include <iostream> #include<cstrin ...

  10. 【数据采集】VBA数据采集可用 COM 组件

    windows 中提供了4个COM组件都可以进行数据采集. Wininet WinHttp XmlHttp MSHTML https://msdn.microsoft.com/en-us/librar ...