zoukankan      html  css  js  c++  java
  • [VJ][bfs]Catch That Cow

    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.

    Examples

    Input

    5 17

    Output

    4

    描述:

    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.

    正确解法:

    以前的都是二维,突然一维有点不会qaq

    不能超范围,n==k时要特殊判断。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 int n, k;
     9 int book[100010];
    10 struct student
    11 {
    12     int x, step;
    13 }que[100010];
    14 bool  check(int x)
    15 {
    16     if (x < 0 || x>100000 || book[x] == 1)
    17         return 0;
    18     return 1;
    19 }
    20 int bfs()
    21 {
    22     if (n == k)    return 0;
    23     book[n] = 1;
    24     int head = 1, tail = 2;
    25     que[head].x = n;
    26     que[head].step = 0;
    27     while (head < tail) {
    28         int tx1 = que[head].x - 1;
    29         if (check(tx1))
    30         {
    31             que[tail].x = tx1;
    32             que[tail].step = que[head].step + 1;
    33             book[tx1] = 1;
    34             if (tx1 == k)
    35                 return que[tail].step;
    36             tail++;
    37         }
    38         int tx2 = que[head].x + 1;
    39         if (check(tx2))
    40         {
    41             que[tail].x = tx2;
    42             que[tail].step = que[head].step + 1;
    43             book[tx2] = 1;
    44             if (tx2 == k)
    45                 return que[tail].step;
    46             tail++;
    47         }
    48         int tx3 = que[head].x * 2;
    49         if (check(tx3))
    50         {
    51             que[tail].x = tx3;
    52             que[tail].step = que[head].step + 1;
    53             book[tx3] = 1;
    54             if (tx3 == k)
    55                 return que[tail].step;
    56             tail++;
    57         }
    58         head++;
    59     }
    60 }
    61 int main()
    62 {
    63     cin >> n >> k;
    64     cout << bfs() << endl;
    65     return 0;
    66 }
    View Code
    No matter how you feel, get up , dress up , show up ,and never give up.
  • 相关阅读:
    mysql优化四(SQL优化)
    mysql优化三(索引)
    mysql优化二(优化数据库的设计)
    mysql优化一(优化方式)
    ES6——Thunk函数
    JavaScript例子
    fetch使用
    ES6—Generator的next()方法传参
    ES6——Generator函数抛出异常
    JavaScript对象添加属性和方法
  • 原文地址:https://www.cnblogs.com/Kaike/p/9914385.html
Copyright © 2011-2022 走看看