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. 如何刷新或清除HttpURLConnection的连接缓存

    项目需要定期与远程服务器同步数据,基于如下代码: URL url = new URL("http://test.com/sales/info"); connection = (Ht ...

  2. Daily Build

    Daily Build 是一件非常有意义的事情,也是敏捷开发中关于 “持续集成” 的一个实践.Daily Build 对于开发来说有如下好处: 保证了每次 check in 的代码可用,不会造成整个工 ...

  3. [Nhibernate]对象状态

    目录 写在前面 文档与系列文章 对象状态 瞬时态(Transient) 持久态(Persistent) 托管态(Detached) 对象状态转换 总结 写在前面 前面两篇文章介绍了SchemaExpo ...

  4. DP专辑

    今天练了一波DP.时间紧迫我就只贴代码了. 20141120 fzu2129 http://acm.fzu.edu.cn/problem.php?pid=2129 不同的子序列个数 //#pragma ...

  5. ASP.NET 系统对象 Request(一)

    Request对象 用来获取客户端在请求一个页面或传送一个Form是提供的所有信息.它包括用户的HTTP变量.能够识别的浏览器.存储客户端的Cookie信息和请求地址等. Request对象是Syst ...

  6. 交叉编译中的build、host和target

    build.host和target    在交叉编译中比较常见的一些参数就是build.host和target了,正确的理解这三者的含义对于交叉编译是非常重要的,下面就此进行解释 --build=编译 ...

  7. word20161214

    MAC, Message Authentication Code / 消息验证代码 MAC address / MAC 地址 machine-centric / 机器中心的 Macintosh-acc ...

  8. Ubuntu16.04 安装MATALAB R2015b教程

    1.安装 将镜像文件内文件解压出来,添加执行权限,否则执行 ./install指令会出错 chmod -R 777 MATALAB 执行如下指令 ./install 2.填入补丁内的密匙 在Matla ...

  9. 数据库模型设计PowerDesigner

    Power Designer 是Sybase公司的CASE工具集,使用它可以方便地对管理信息系统进行分析设计,他几乎包括了数据库模型设计的全过程.利用Power Designer可以制作数据流程图.概 ...

  10. js 连续赋值。。理解不了,先占坑

    http://www.cnblogs.com/xxcanghai/p/4998076.html