zoukankan      html  css  js  c++  java
  • 2016HUAS暑假集训训练题 B

                                                                                                                        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搜索
                                                  
    依次把得到的数存入计数数组得到到达它所需的步数  再一个一个找直到找到牛就是最小步数
     
     
     1 import java.io.BufferedInputStream;
     2 
     3 import java.util.Arrays;
     4 
     5 import java.util.Scanner;
     6 
     7 public class Main {
     8     public static void main(String[] args) {
     9         Scanner s = new Scanner(new BufferedInputStream(System.in));
    10         while (s.hasNext()) {
    11             int a = s.nextInt(), b = s.nextInt();
    12             int[] l = new int[100010];
    13             int starts = 0;
    14             int ends = 0;
    15             l[0] = a;
    16             int n[] = new int[100010];
    17             Arrays.fill(n, 0);
    18             while (true) {
    19                 int v = l[starts];
    20                 if (v == b)
    21                     break;
    22                 if (n[v+ 1] == 0 && v + 1 <= 100000) {
    23                     l[++ends] = v + 1;
    24                     n[v + 1] = n[v] + 1;
    25                 }
    26                 if (v - 1 >= 0 && n[v - 1] == 0) {
    27                     l[++ends] = v - 1;
    28                     n[v - 1] = n[v] + 1;
    29                 }
    30                 if (v * 2 <= 100000 && n[v * 2] == 0) {
    31                     l[++ends] = v * 2;
    32                     n[v * 2] = n[v] + 1;
    33                 }
    34                 starts++;
    35             }
    36             System.out.println(n[b]);
    37         }
    38         s.close();
    39     }
    40 }

      

  • 相关阅读:
    Git切换分支
    JS中字符串那些事~
    将博客搬至CSDN
    MFC默认窗口类名称
    Windows下使用vim编写代码,使用nmake编译代码,使用vs来调试代码
    从CWnd::GetSafeHwnd实现得到的知识
    ctags使用
    MCI支持的格式在注册表中的位置
    注意!!一定要谨慎使用c/c++原生指针
    MFC模态对话框程序不响应OnIdle
  • 原文地址:https://www.cnblogs.com/LIUWEI123/p/5676678.html
Copyright © 2011-2022 走看看