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]);
}
}

最新文章

  1. Npm包的开发
  2. java JDK8 学习笔记——助教学习博客汇总
  3. Java环境变量配置&amp;解决版本不一致问题
  4. Android(java)学习笔记235:多媒体之计算机图形表示方式
  5. 网络基础知识HTTP(1) --转载
  6. 转--Windows下将jar包封装成服务程序
  7. BC 2015在百度之星程序设计大赛 - 预赛(1)(KPI-树董事长)
  8. 【基础】Attribute的妙用
  9. Django之modelform组件
  10. day4 liaoxuefeng--调试、线程、正则表达式
  11. windows下网络编程UDP
  12. How to delete VSTS Project
  13. ES6躬行记(8)——数字
  14. IE9下table th不显示边框解决方法
  15. HDOJ2025_查找最大元素
  16. 工作流管库的bpmn部署在数据库中
  17. 中间人攻击工具mitmf(另类的XSS注入攻击)
  18. 【瞎搞题】gym226123 L. For the Honest Election
  19. 高级数据类型--列表[list]
  20. python show slave status

热门文章

  1. H3C DHCP中继工作原理
  2. js 实用技巧 短路求值
  3. Jquery Validate表单验证,自定义校验正整数
  4. C#面试题整理(不带答案)
  5. Oracle单引号拼接和替换
  6. vagrant在windows下的安装和配置(一)
  7. hibernate配置文件模板
  8. C++,Windows/MFC_中L和_T()之区别
  9. 第二阶段:流程图:8.axure绘制简单业务流程图
  10. socket粘包问题及解决方案