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. Android:res之shape制作圆角、虚线、渐变

    xml控件配置属性 android:background="@drawable/shape" 标签 corners ----------圆角gradient ----------渐 ...

  2. android应用程序如何调用支付宝接口

    最近在做一个关于购物商城的项目,项目里面付款这块我选的是调用支付宝的接口,因为用的人比较多. 在网上搜索了以下,有很多这方面的教程,但大部分教程过于陈旧,而且描述的过于简单.而且支付宝提供的接口一直在 ...

  3. git学习 git-flow

    例子 初始化 在git init之后执行git init flow;接下来的命名约定建议使用默认值; 新特性 为即将发布的版本开发新功能特性. 这通常只存在开发者的库中. 增加新特性 git flow ...

  4. http://blog.csdn.net/z69183787/article/details/37819831

    http://blog.csdn.net/z69183787/article/details/37819831

  5. Linux中cp覆盖不提示

    cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,这在大量cp覆盖操作的时候是不能忍受的. 1. 把a目录下的文件复制到b目录 cp –r a/* b 2. 执行上面的命令时,b存在的每个文件都会 ...

  6. C#获取IP地址

    public string GetUserIP()   {        string _userIP;       if(Request.ServerVariables["HTTP_VIA ...

  7. PHP如何实现文件上传

    PHP如何实现文件上传 1.表单部分  允许用户上传文件,在HTML表单的声明中要加上一个上传的属性:  enctype = 'multipart/form-data'  表单的method必须是PO ...

  8. Codeforces Round #336 (Div. 2)

    水 A - Saitama Destroys Hotel 简单的模拟,小贪心.其实只要求max (ans, t + f); #include <bits/stdc++.h> using n ...

  9. kafka 集群安装与安装测试

    一.集群安装 1. Kafka下载:wget https://archive.apache.org/dist/kafka/0.8.1/kafka_2.9.2-0.8.1.tgz 解压 tar zxvf ...

  10. java中的URLConnection和HttpURLConnection

    URL url = new URL(strUrl); URLConnection con = url.openConnection(); URL url = new URL(strUrl); Http ...