• bfs—Catch That Cow—poj3278


    Catch That Cow
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 87152   Accepted: 27344

    http://poj.org/problem?id=3278

    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,题目是单测试例,写的是多测试例。
     1 #include<iostream>
     2 #include<queue>
     3 #include<cstring>
     4 using namespace std;
     5 
     6 const int MAX=100000;
     7 int pace[MAX+10];
     8 int main()
     9 {
    10     int n,k;
    11     while(cin>>n>>k)
    12     {
    13         memset(pace,0,sizeof(pace));
    14         int t;
    15         queue<int> qu;
    16         qu.push(n);
    17         while(!qu.empty())
    18         {
    19             t=qu.front();
    20             if(t==k)
    21             {
    22                 cout<<pace[t]<<endl;
    23                 return 0;
    24             }
    25             qu.pop();
    26             if(t<MAX&&!pace[t+1])
    27             {
    28                 qu.push(t+1);
    29                 pace[t+1]=pace[t]+1;
    30             }
    31             if(t>0&&!pace[t-1])
    32             {
    33                 qu.push(t-1);
    34                 pace[t-1]=pace[t]+1;
    35             }
    36             if(t*2<=MAX&&!pace[t*2])
    37             {
    38                  qu.push(t*2);
    39                  pace[t*2]=pace[t]+1;
    40             }
    41         }
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    常见错误集锦
    auto 迭代器的使用
    案例:带有动画的返回顶部
    案例:toDoList
    jQuery事件
    案例:发布微博功能
    案例:购物车功能模块
    jQuery常用的API
    案例:王者荣耀手风琴效果
    案例:jQuery实现tab栏切换功能
  • 原文地址:https://www.cnblogs.com/Fresh--air/p/6642837.html
Copyright © 2020-2023  润新知