• 暑假集训(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
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    VMware NAT 静态IP模式下上网
    无法连接虚拟设备 ide1:0
    C# ContainsKey与TryGetValue方法探究
    深入讲解以太坊的数据存储
    sqlserver之查询数据插入新建表@已存在表(oracle)
    python之列表、元祖、字典和数组的使用
    python之列表、元组、字典、数组对比
    python之ConfigParser介绍
    从业人员证券交易行为合规管理测试
    window系统自动设置时间同步脚本
  • 原文地址:https://www.cnblogs.com/huas-zlw/p/5676700.html
Copyright © 2020-2023  润新知