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 X - 1 or X + 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.
 
题意 : 农民有3种走路方式 , 问怎么走可以最短到达目标位置。
 
思路 : BFS 走感觉还是很好想到的 , 之前一直有一个地方不是很理解 ,就是搜素图的时候什么要标记走过的点什么时候不标记走过的点,现在差不多懂点了 ,每走一步,就把当前这步所有可以到达的位置全部找到,之前走过的点不计入。还有在求步数的搜索题中,特意建一个数组,当前位置的步数等于上一次所走的步数 +1 。
 
代码 :
/*
* Author: ry
* Created Time: 2017/10/26 9:33:23
* File Name: 2.cpp
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <time.h>
using namespace std;
const int eps = 1e5+5;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
#define Max(a,b) a>b?a:b
#define Min(a,b) a>b?b:a
#define ll long long
int n, k;
int bu[eps]; void bfs(){
queue<int>que;
que.push(n);
//memset(vis, 0, sizeof(vis));
memset(bu, -1, sizeof(bu)); bu[n] = 0;
while (!que.empty()){
int a = que.front();
que.pop(); if (a == k) break;
for(int i = 0; i < 3; i++){
if (i == 0) {
int b = a - 1;
if (b >= 0 && b < eps && bu[b] == -1){
que.push(b);
bu[b] = bu[a] + 1;
}
}
else if (i == 1){
int b = a + 1;
if (b >= 0 && b < eps && bu[b] == -1){
que.push(b);
bu[b] = bu[a] + 1;
}
}
else {
int b = 2*a;
if (b >= 0 && b < eps && bu[b] == -1){
que.push(b);
bu[b] = bu[a] + 1;
}
}
}
} } int main (){ while (~scanf("%d%d", &n, &k)){
bfs();
//for(int i = 1; i <= k; i++){
//printf("%d\t", bu[i]);
//}
//printf("\n");
printf("%d\n", bu[k]);
}
}

BFS - 求最短路径的更多相关文章

  1. UVa 1599 理想路径(反向BFS 求最短路径 )

    题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...

  2. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  3. POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)

    POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...

  4. POJ 3126 Prime Path(BFS求“最短路”)

    题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...

  5. Spfa求最短路径

    spfa求最短路径,其思想就是遍历每一个点,将没有入队的点入队,从这个点开始不断修改能够修改的最小路径,直到队空.不过这里一个点可以重复入队. 这个需要有存图的基础--------->前向星存图 ...

  6. BFS求最短路

    假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...

  7. 6.4.2 用BFS求最短路

    前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...

  8. C++迪杰斯特拉算法求最短路径

    一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...

  9. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

随机推荐

  1. python模块之序列化模块

    序列化 """ 序列--字符串 序列化--其他数据类型转化为字符串数据类型 反序列化--字符串转化为其他数据类型 """ json模块 &q ...

  2. 免费开源3D模型设计软件汇总

    免费开源3D模型设计软件汇总 3D 打印需要先通过计算机辅助设计(CAD)进行建模,再将建好的3D模型“分割”成逐层的截面,从而指导3D打印机进行逐层打印.因此用于3D打印的3D模 型大都储存或输出成 ...

  3. 2018-9-2-WPF-开发自动删除软件

    title author date CreateTime categories WPF 开发自动删除软件 lindexi 2018-09-02 14:51:48 +0800 2018-08-09 09 ...

  4. H3C 示例:根据子网数划分子网

  5. 使用Python内置的smtplib包和email包来实现邮件的构造和发送。

    此文章github地址:https://github.com/GhostCNZ/Python_sendEmail Python_sendEmail 使用Python内置的smtplib包和email包 ...

  6. 深度解读 - TDD(测试驱动开发)

    转自:http://www.jianshu.com/p/62f16cd4fef3 本文结构: 什么是 TDD 为什么要 TDD 怎么 TDD FAQ 学习路径 延伸阅读 什么是 TDD TDD 有广义 ...

  7. SVG路径无法识别问题

    SVG 路径不规范无法识别 使用 (?<=(,|-))\. 替换为0. 即可

  8. CFEducational Codeforces Round 66题解报告

    CFEducational Codeforces Round 66题解报告 感觉丧失了唯一一次能在CF上超过wqy的机会QAQ A 不管 B 不能直接累计乘法打\(tag\),要直接跳 C 考虑二分第 ...

  9. Maven 运行 tomcat:run 时出现 Unable to compile class for JSP...

    近来无事便去看了看神奇的 Maven , 但写第一个 Hello,World 就非常不友好的怼给我一个 500, 很是郁闷; 开发环境: JDK1.8, Maven 3.5 项目目录: \maven_ ...

  10. 2018-2-13-win10-uwp-模拟网页输入

    title author date CreateTime categories win10 uwp 模拟网页输入 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 1 ...