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,三个状态分别为x-1,x+1,2*x,注意剪枝一些不太可能的状态,代码如下:
const int maxm = ;

struct Node {
int times, x;
Node(int _times, int _x):times(_times),x(_x) {}
}; int vis[maxm], n, k; int main() {
scanf("%d%d", &n, &k);
queue<Node> q;
q.push(Node(, n));
while(!q.empty()) {
Node tmp = q.front();
q.pop();
if(vis[tmp.x])
continue;
vis[tmp.x] = ;
if(tmp.x == k) {
printf("%d\n", tmp.times);
break;
}
tmp.times++;
if(tmp.x && !vis[tmp.x-])
q.push(Node(tmp.times, tmp.x - ));
if(!vis[tmp.x+])
q.push(Node(tmp.times, tmp.x + ));
if(tmp.x < k * && !vis[tmp.x*])
q.push(Node(tmp.times, tmp.x * ));
}
return ;
}

Day2-E-Catch That Cow-POJ3278的更多相关文章

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

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

  2. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

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

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

  4. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  5. POJ3278——Catch That Cow(BFS)

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

  6. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  7. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  8. POJ3278——Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 114140   Accepted: 35715 ...

  9. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  10. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

随机推荐

  1. JAVA(1)之关于对象数组作形参名的方法的使用

    public class Test{ int tour; public static void cs(Test a[]) { for (int i = 0; i < a.length; i++) ...

  2. 吴裕雄--天生自然Numpy库学习笔记:NumPy IO

    Numpy 可以读写磁盘上的文本数据或二进制数据. NumPy 为 ndarray 对象引入了一个简单的文件格式:npy. npy 文件用于存储重建 ndarray 所需的数据.图形.dtype 和其 ...

  3. 吴裕雄 python 神经网络——TensorFlow训练神经网络:花瓣识别

    import os import glob import os.path import numpy as np import tensorflow as tf from tensorflow.pyth ...

  4. 有个网站秒破mdb访问密码

    http://tools.bugscaner.com/crackmdb/网站

  5. 解决maven 在intellij IDEA 下载依赖包速度慢的问题

    解决maven 在intellij IDEA 下载依赖包速度慢的问题 参考:https://www.jianshu.com/p/63a593700ebc

  6. Update(Stage4):sparksql:第5节 SparkSQL_出租车利用率分析案例

    目录: 1. 业务2. 流程分析3. 数据读取5. 数据清洗6. 行政区信息 6.1. 需求介绍 6.2. 工具介绍 6.3. 具体实现7. 会话统计 导读 本项目是 SparkSQL 阶段的练习项目 ...

  7. 高级T-SQL进阶系列 (一)【上篇】:使用 CROSS JOIN 介绍高级T-SQL

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文连接:传送门 这是一个新进阶系列的第一篇文章,我们将浏览Transact-SQL(T-SQL)的更多高级特性.这个进阶系列将会包 ...

  8. 「AT4741 [ABC132D] Blue and Red Balls」

    题目大意 给出一个长度为 \(N\) 的01串,其中有 \(K\) 个 \(1\),其他都是 \(0\),需要求出当着 \(K\) 个 \(1\) 分成 \(1\) 到 \(K\) 段每一个的方案数. ...

  9. DBC的故事(二)

    上篇介绍了MSB和LSB,此篇介绍更复杂的:有符号和无符号数. 1.信号符号 CAN信号有其物理意义,如温度.扭矩等,这些信号是有负值的,常见的解决方案有2种: 1)把offset设成负值 如温度of ...

  10. Centos 下安装php

    1 从php 官网下载源安装包 http://php.net/downloads.php // 安装php 扩展 2 yum install libxml2 libxml2-devel openssl ...