• Catch That Cow HDU


    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?

    InputLine 1: Two space-separated integers: N and KOutputLine 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.
    科幻大片BFS系列
     1 #include <iostream>
     2 using namespace std;
     3 #include<string.h>
     4 #include<set>
     5 #include<stdio.h>
     6 #include<math.h>
     7 #include<queue>
     8 #include<map>
     9 #include<algorithm>
    10 #include<cstdio>
    11 #include<cmath>
    12 #include<cstring>
    13 struct lll
    14 {
    15     int x,bu;
    16 }s;
    17 
    18 int max1=210000;
    19 int main()
    20 {
    21 
    22     int n,m;
    23     while(cin>>n>>m)
    24     {
    25         map<int, int >a;
    26         queue<lll >TM;
    27         s.x=n;
    28         s.bu=0;
    29         a[s.x]=1;
    30         TM.push(s);
    31         while(!TM.empty())
    32         {
    33             if(TM.front().x==m)
    34                 break;
    35             s.x=TM.front().x+1;
    36             s.bu=TM.front().bu+1;
    37             if(s.x>=0&&s.x<max1&&a[s.x]==0)
    38             {
    39                 a[s.x]=1;
    40                 TM.push(s);
    41             }
    42             s.x=TM.front().x-1;
    43             s.bu=TM.front().bu+1;
    44             if(s.x>=0&&s.x<max1&&a[s.x]==0)
    45             {
    46                 a[s.x]=1;
    47                 TM.push(s);
    48             }
    49             s.x=TM.front().x*2;
    50             s.bu=TM.front().bu+1;
    51             if(s.x>=0&&s.x<max1&&a[s.x]==0)
    52             {
    53                 a[s.x]=1;
    54                 TM.push(s);
    55             }
    56             TM.pop();
    57         }
    58         cout<<TM.front().bu<<endl;
    59     }
    60     return 0;
    61 }
    View Code
  • 相关阅读:
    《需求工程--软件建模与分析》读书笔记一
    软件工程概论课程总结及给老师的意见
    梦断代码阅读笔记之三
    梦断代码阅读笔记之二
    第二阶段小组冲刺第七天总结
    软件工程第十一周学习进度条
    用户场景描述
    软件工程第九、十周学习进度条
    个人工作总结
    软件工程第八周学习进度条
  • 原文地址:https://www.cnblogs.com/dulute/p/7272716.html
Copyright © 2020-2023  润新知