zoukankan      html  css  js  c++  java
  • 暑假集训(1)第二弹 -----Catch the cow(Poj3278)

    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 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?

    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.
     
     
    问题分析:
    简单bfs可以解答,分支为step+1;step-1;以及step*2. 第一次使用bfs以及容器;据说用容器比较慢,有时间想一想替代的方法0.0
     
     1 #include "iostream"
     2 #include "queue"
     3 
     4 using namespace std;
     5 char a[100010];
     6 void mset()
     7 {
     8     for (int i=0;i<100000;i++)
     9          a[i] = '1';
    10 }
    11 struct far
    12 {
    13     int w;
    14     int step;
    15 };
    16 int bfs(int n,int k)
    17 {
    18    if (n>=k)
    19         return n-k;
    20     queue<far> q;
    21     far fir,sec;
    22     fir.w = n;
    23     fir.step =0;
    24     a[n] = '0';
    25     q.push(fir);
    26     while (!(q.empty()))
    27     {
    28        sec=q.front();
    29        q.pop();
    30        for (int i=1;i<=3;i++)
    31        {
    32              switch (i)
    33               {
    34                 case 1 : fir.w=sec.w+1; break;
    35                 case 2 : fir.w=sec.w-1; break;
    36                 case 3 : fir.w=sec.w*2; break;
    37               }
    38               fir.step=sec.step+1;
    39               if (fir.w == k)   return fir.step;
    40               if (fir.w>=0 && fir.w<=100000 && a[fir.w] == '1' )
    41              {
    42                 a[fir.w] ='0';
    43                 q.push(fir);
    44              }
    45        }
    46     }
    47 }
    48 int main()
    49 {
    50    int n,k;
    51    while (cin>>n>>k)
    52    {
    53        mset();
    54        cout<<bfs(n,k)<<endl;
    55    }
    56     return 0;
    57 }
    View Code
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    C#调用C++代码遇到的问题总结
    ASP.NET MVC Controller的激活
    利用fis3自动化处理asp.net项目静态资源时遇到的一个编码问题
    工作中遇到的一个多线程下导致RCW无法释放的问题
    在mongoose中使用$match对id失效的解决方法
    passport源码研究
    android手机旋转屏幕时让GridView的列数与列宽度自适应
    git 版本回退
    梦之解读:如何成为牛人
    DataTable与DTO对象的简易转换类
  • 原文地址:https://www.cnblogs.com/huas-zlw/p/5676700.html
Copyright © 2011-2022 走看看