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. PyQt5日历控件及相关操作

    1.日历控件QCalendarWidget控件import sys,mathfrom PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQ ...

  2. SpringBoot与Mybatis整合(包含generate自动生成代码工具,数据库表一对一,一对多,关联关系中间表的查询)

    链接:https://blog.csdn.net/YonJarLuo/article/details/81187239 自动生成工具只是生成很单纯的表,复杂的一对多,多对多的情况则是在建表的时候就建立 ...

  3. 基于three.js实现特定Div容器的粒子特效封装

    本文基于three.js实现特定容器的粒子特效效果,支持用户传入特定的dom对象以及粒子颜色. 效果图 移入库 <script src="jquery-1.11.3.min.js&qu ...

  4. mysql字符串相关函数(并与sql server对比)

    https://blog.csdn.net/zhengxiuchen86/article/details/81220779 1.判断子串substr在字符串str中出现的位置 例子:查询']'在‘OP ...

  5. C语言的变量存储方式和生存期

    2020.2.28日,封城一个多月了,紧邻毕业期,我在家抽空学习一下C. 看到了变量的存储方式和生存期这一章节,下面就是我整理的内容 下面是用于理解静态局部变量这个概念所写的代码,主要是需要分析一下函 ...

  6. selenium webdriver 小计

    getText(),获得标签内文本 getAttribute("title")获得对应的html属性值

  7. centos 7中添加一个新用户并授权的步骤详解

    1.创建新用户: 创建一个用户名为:zhangbiao adduser zhangbiao 为这个用户初始化密码,linux会判断密码复杂度,不过可以强行忽略: passwd zhangbiao  更 ...

  8. navicat12破解详细教程

    以管理员身份运行此注册机: 运行注册机 打开注册机后,1) Patch勾选Backup.Host和Navicat v12,然后点击Patch按钮: 默认勾选 找到Navicat Premium 12安 ...

  9. c++刷算法的好处

    写再最前面:摘录于柳神的笔记 在已经学习过C语⾔的前提下,学习C++并使⽤它刷算法的学习成本⾮常低-只需要⼏个⼩时就可 以学会- C++向下兼容C,C语⾔⾥⾯的语法完全可以在C++⽂件中运⾏,所以学习 ...

  10. 46 求1+2+3+...+n 静态成员函数和静态变量

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 思路: 1)使用构造函数的方法,需要使用sta ...