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. 【codeforces 766B】Mahmoud and a Triangle

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. java 利用TCP上传文件

    从客户端上传到服务器端,其实本质上也就是复制! package july76net; //上传文件(文本) import java.io.BufferedReader; import java.io. ...

  3. 人脸检测MTCNN的训练过程(PRO网络)

    以下学习均由此:https://github.com/AITTSMD/MTCNN-Tensorflow 数据集 WIDER Face for face detection and Celeba for ...

  4. visual studio 2010问题修复

    我在重新安装 Visual Studio 2010 和 SQL sever 2012 的时候,安装好的两个软件打开时都遇到了这个问题:“在此计算机中仅有部分 Microsoft Visual Stud ...

  5. C# 通过 probing 指定 dll 寻找文件夹

    在很大的项目开发,会发现项目引用的 dll 会很多,我想要按照不同的功能,将不同的 dll 放在不同的文件夹 简单的方法是通过修改 App.config 文件指定文件夹,如将文件移动到 abc\12 ...

  6. 2018-2-13-win10-uwp-图标制作器

    title author date CreateTime categories win10 uwp 图标制作器 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17 ...

  7. 2018-8-10-win10-uwp-Window.Current.Dispatcher中Current为null

    title author date CreateTime categories win10 uwp Window.Current.Dispatcher中Current为null lindexi 201 ...

  8. HDU4609 FFT+组合计数

    HDU4609 FFT+组合计数 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4609 题意: 找出n根木棍中取出三根木棍可以组成三角形的概率 题解: ...

  9. 树莓派4安装net core3.0环境

    树莓派4官方系统是32系统,所以需要安装arm32版本的net core sk和runtime 1,首先创建一个文件夹 dotnet-arm32 sudo mkdir dotnet arm32 2,下 ...

  10. Memcahced 缓存过期时间问题

    转载:https://help.aliyun.com/knowledge_detail/38654.html 关于设置缓存数据的过期时间,可以参考以下Memcached官方说明: An expirat ...