BFS - 求最短路径
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
N and
K
Output
Sample Input
5 17
Sample Output
4
Hint
/*
* 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 - 求最短路径的更多相关文章
- UVa 1599 理想路径(反向BFS 求最短路径 )
题意: 给定一个有重边有自环的无向图,n个点(2 <= n <= 100000), m条边(1 <= m <= 200000), 每条边有一个权值, 求从第一个点到n的最少步数 ...
- 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 ...
- POJ 2251 Dungeon Master --- 三维BFS(用BFS求最短路)
POJ 2251 题目大意: 给出一三维空间的地牢,要求求出由字符'S'到字符'E'的最短路径,移动方向可以是上,下,左,右,前,后,六个方向,每移动一次就耗费一分钟,要求输出最快的走出时间.不同L层 ...
- POJ 3126 Prime Path(BFS求“最短路”)
题意:给出两个四位数的素数,按如下规则变换,使得将第一位数变换成第二位数的花费最少,输出最少值,否则输出0. 每次只能变换四位数的其中一位数,使得变换后的数也为素数,每次变换都需要1英镑(即使换上的数 ...
- Spfa求最短路径
spfa求最短路径,其思想就是遍历每一个点,将没有入队的点入队,从这个点开始不断修改能够修改的最小路径,直到队空.不过这里一个点可以重复入队. 这个需要有存图的基础--------->前向星存图 ...
- BFS求最短路
假设有一个n行m列的迷宫,每个单位要么是空地(用1表示)要么是障碍物(用0表示).如和找到从起点到终点的最短路径?利用BFS搜索,逐步计算出每个节点到起点的最短距离,以及最短路径每个节点的前一个节点. ...
- 6.4.2 用BFS求最短路
前面的篇幅占了太多,再次新开一章,讲述BFS求最短路的问题 注意此时DFS就没有BFS好用了,因为DFS更适合求全部解,而BFS适合求最优解 这边再次提醒拓扑变换的思想在图形辨认中的重要作用,需要找寻 ...
- C++迪杰斯特拉算法求最短路径
一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...
随机推荐
- 同一个页面 andriod和ios设备上的按钮颜色不一致
andriod系统显示蓝色的按钮,正常:ios设备显示灰色的按钮,不正常. style属性添加-webkit-appearance: none;
- 前端css图片固定宽高问题
img需要宽高都固定时,图片往往会因此变形,此时可采用的方法有: 上述代码会使得图片居中,边缘部分不显示.这是在图片大小跟container大小差不多的情况下.如果图片很大的话,只显示中心部分是不行的 ...
- Delphi中的Val函数和iif函数(出错的时候,会有索引值)
在delphi中Val是一个将字符串转换为数字的函数,Val(S; var V; var Code: Integer)第一个参数是要转换的字符串,第二个参数存放转换后的数字,可以是整数或浮点数,第三个 ...
- C++中 =default 和 =delete 使用
编译器默认为一个类生成的默认函数 默认构造函数 默认析构函数 默认拷贝构造函数 默认赋值函数 移动构造函数 移动拷贝函数 class DataOnly { public: DataOnly () // ...
- Excel基本功能
公式基础: 比较运算符的种类 flase对应0 而ture对应1 连接运算 利用之前提到的ture就是1 乘以100 注意用括号区分优先级 函数应用基础: 系统已经列好这几个常用的函数 右键单击状态栏 ...
- 从0开始.NET CORE认证
引子 最近在学习IdentityServer4,看了园子里大神们的文章,但是看完之后,能明白这样做可以达到业务需求.但是为什么这样做可以达到业务需求,我用其他方式不行吗?为什么这样做可以呢.也就是老话 ...
- DEVOPS技术实践_23:判断文件下载成功作为执行条件
在实际生产中,我们经常会需要通过判断一个结果作为一个条件去执行另一个内容,比如判断一个文件是否存在,判官一个命令是否执行成功等等 现在我们选择其中一个场景进行实验,当某个目录下存在,则执行操作 1. ...
- windows10 powershell上切换至cmd
前言 在windows10 上是遇到了坑,因为出现了这样的情况!不要说什么盗版,公司买的正版呢. 上图是powershell,下图是 cmd,然后我同样使用powershell 与 cmd,查询nod ...
- 从头学pytorch(十一):自定义层
自定义layer https://www.cnblogs.com/sdu20112013/p/12132786.html一文里说了怎么写自定义的模型.本篇说怎么自定义层. 分两种: 不含模型参数的la ...
- 调用微软未公开ZwQueryInformationThread函数根据线程句柄获取线程ID
这段时间公司项目中为了支持XP系统同事代码中用到了 GetThreadId 这个微软的API 但是这个API最低支持版本是 Windows version Windows Vista [desktop ...