Description

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.





题意:在一维的直线上,给出John的位置n,和cow的位置k,求出John按照给定三种规则找到cow的最少步数。

分析:用广度优先搜索从源点开始,依次用三种规则进行查找,设置边界条件,最先找到的即为最优解。这里还要注意下数组边界大小要比用于比较的边界要大一点,否则会数组越界而RE,我在这里吃了6个RE,一直没找到原因。后来才惊奇地发现时用于比较的MAX和数组大小Max相同。

import java.util.LinkedList;
import java.util.Scanner; public class Main {
static int start, end;
static int MAX = 200000;
static LinkedList<Integer> q;
static boolean[] visited;
static int[] step;
static int t, next; static int bfs() { q.add(start);
visited[start] = true;
step[start] = 0; while (!q.isEmpty()) { t = q.poll(); for (int i = 0; i < 3; i++) { if (i == 0) {
next = t - 1;
} else if (i == 1) {
next = t + 1;
} else {
next = t * 2;
} if (next > MAX || next < 0)
continue; if (!visited[next]) {
q.add(next);
step[next] = step[t] + 1;
visited[next] = true;
} if (next == end)
return step[next];
}
}
return -1;
} public static void main(String[] args) {
Scanner sc = new Scanner(System.in); q = new LinkedList<Integer>();
//在这个地方比MAX多加上5,放在RE
visited = new boolean[MAX+5];
step = new int[MAX+5]; start = sc.nextInt();
end = sc.nextInt(); if (start >= end) {
System.out.println(start - end);
} else {
System.out.println(bfs());
} }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

最新文章

  1. 【WPF系列】基础学习-XAML
  2. 【分享】图解Windows Server 2012 R2 配置IIS 8全过程
  3. HDInsight 路径问题
  4. 《Linux内核设计与实现》读书笔记(二十)- 补丁, 开发和社区
  5. spring mvc+myBatis配置详解
  6. 前端代码新写法——Zen Coding
  7. ThinkPHP中的模型
  8. Office在线预览及PDF在线预览的实现方式史上最全大集合
  9. linux 建库,编码,导入数据
  10. BZOJ 2743: [HEOI2012]采花( 离线 + BIT )
  11. MFC如何生成一个可串行化的类
  12. [COGS 0014][网络流24题] 搭配飞行员
  13. 对html语义化的理解
  14. 详解ebs接口之客户配置文件导入(二)
  15. shell-计算虚拟机创建时间
  16. java基础(四)-----抽象类与接口
  17. vue-resource的使用,前后端数据交互
  18. Eclipse报错:!!MESSAGE Job found still running.......
  19. Python3-IO模型
  20. 使用Numpy实现卷积神经网络(CNN)

热门文章

  1. 结合canvas做雨滴特效
  2. Android系统--输入系统(二)必备Linux知识_实现inotify_epoll.c
  3. tensorFlow 神经网络2
  4. 简介web服务器的工作原理
  5. DL四(预处理:主成分分析与白化 Preprocessing PCA and Whitening )
  6. js的介绍
  7. java:管道流(线程间管道流)
  8. ROC曲线是通过样本点分类概率画出的 例如某一个sample预测为1概率为0.6 预测为0概率0.4这样画出来,此外如果曲线不是特别平滑的话,那么很可能存在过拟合的情况
  9. JProfiler远程监控 -转
  10. Vue中mixin的用法