B - Catch That Cow

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 - 1 or + 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想要抓一头牛,告诉你john的位置和牛的位置 john有三种移动方式,分别为+1,-1,*2  求抓到牛的最小步数;本题考虑用bfs搜索
                                              
依次把得到的数存入计数数组得到到达它所需的步数  再一个一个找直到找到牛就是最小步数
 
 
 import java.io.BufferedInputStream;

 import java.util.Arrays;

 import java.util.Scanner;

 public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedInputStream(System.in));
while (s.hasNext()) {
int a = s.nextInt(), b = s.nextInt();
int[] l = new int[100010];
int starts = 0;
int ends = 0;
l[0] = a;
int n[] = new int[100010];
Arrays.fill(n, 0);
while (true) {
int v = l[starts];
if (v == b)
break;
if (n[v+ 1] == 0 && v + 1 <= 100000) {
l[++ends] = v + 1;
n[v + 1] = n[v] + 1;
}
if (v - 1 >= 0 && n[v - 1] == 0) {
l[++ends] = v - 1;
n[v - 1] = n[v] + 1;
}
if (v * 2 <= 100000 && n[v * 2] == 0) {
l[++ends] = v * 2;
n[v * 2] = n[v] + 1;
}
starts++;
}
System.out.println(n[b]);
}
s.close();
}
}

  

2016HUAS暑假集训训练题 B - Catch That Cow的更多相关文章

  1. 2016huas暑假集训训练题 G-Who's in the Middle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/G 此题大意是给定一个数n 然后有n个数 要求求出其中位数  刚开始以为是按数学中的 ...

  2. 2016HUAS暑假集训训练题 G - Oil Deposits

    Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...

  3. 2016HUAS暑假集训训练题 F - 简单计算器

    Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值.    Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运 ...

  4. 2016HUAS暑假集训训练题 E - Rails

    There is a famous railway station in PopPush City. Country there is incredibly hilly. The station wa ...

  5. 2016HUAS暑假集训训练题 D - Find a way

    F                                                                                                   ...

  6. 2016HUAS暑假集训训练2 O - Can you find it?

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/O 这道题是一道典型二分搜素题,题意是给定3个数组 每个数组的数有m个 再给定l个s ...

  7. 2016HUAS暑假集训训练2 L - Points on Cycle

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/L 这是一道很有意思的题,就是给定一个以原点为圆心的圆,然后给定 一个点  求最大三 ...

  8. 2016HUAS暑假集训训练2 K - Hero

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/K 这也是一道贪心题,刚开始写时以为只要对每一敌人的攻击和血的乘积进行从小到大排序即 ...

  9. 2016HUAS暑假集训训练2 J - 今年暑假不AC

    题目链接:http://acm.hust.edu.cn/vjudge/contest/121192#problem/J 此题要求是计算能够看到最多的节目 ,贪心算法即可,首先对结束时间排序,然后在把开 ...

随机推荐

  1. zookeeper源码分析(一) 工作原理

    来自:http://www.codedump.info/?p=207 阅读zookeeper代码一段时间(注:是很长一段时间,断断续续得有半年了吧?)之后,我要开始将一些积累下来的东西写下来了,鉴于我 ...

  2. Servlet获取类路径下的资源

    示例程序: package cn.yzu; import java.io.IOException; import java.io.InputStream; import javax.servlet.S ...

  3. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  4. SoapUI之webservice接口测试(一)

    1.新建soap project 添加后出现接口内容 2.为了方便后续的测试,以防某些参数删除错了,这边需要新建测试集 3.点开新建的测试集可以发现,里面的内容跟原始测试集内容是一样的 然后就可以在这 ...

  5. Android学习系列(43)--使用事件总线框架EventBus和Otto

    事件总线框架 针对事件提供统一订阅,发布以达到组件间通信的解决方案. 原理 观察者模式. EventBus和Otto 先看EventBus的官方定义: Android optimized event ...

  6. 《DSP using MATLAB》示例Example4.7

    ROC分三种情况:

  7. 页面内容排序插件jSort的使用

        当页面列表内容很多的时候,我们可能需要将内容按照某个方式进行排序,比如按照字母或者大小等排序.本文将使用排序插件jSort来对页面内容进行排序. jSort插件可以对页面任何内容进行排序(ta ...

  8. [bzoj2118]墨墨的等式【dijk+堆】

    10/30的update:如果是冲着dijk的板子来的,建议看多校联考contest中第二场day2的T2,那边的写法比较优秀... --------------------------------- ...

  9. ccc let

    let,其实就是块级作用域申明变量的var.之前JS的var关键字是非块级作用域的,而是函数级的. 例如arr=[0,1,2],我们经常写循环 for(var i=0,len=arr.length; ...

  10. ccc animation

    cc.Class({ extends: cc.Component, properties: { sheepAnim: { default: null, type: cc.Animation } }, ...