• POJ3287(BFS水题)


    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.
    题意:

    FJ要抓奶牛。

             开始输入N(FJ的位置)K(奶牛的位置)。

             FJ有三种移动方法:1、向前走一步,耗时一分钟。

                                                  2、向后走一步,耗时一分钟。

                                                 3、向前移动到当前位置的两倍N*2,耗时一分钟。

           问FJ抓到奶牛的最少时间(奶牛不动)

    解题思路:很明显我们求最短的时间就相当于求走的最少步数,所以我们就很容易想到用广搜BFS了,把每次FJ可以到的位置都压入队列,然后用一个vis数组标记FJ是否已经去过该位置了,再用 一个step数组存储FJ到该位置所需要的最短时间即可。要注意的是,他的位置也有范围是0-100000,开始我就是因为右区间少了个=号,WA了好几次,所以还是要细心。难的做不出只好做简单的了。。。

    附上代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 int vis[100005];  //标记是否走过 
     7 int step[100005]; //储存到该处的最短时间 
     8 int n,k;
     9 queue<int> que;
    10 int ans;
    11 
    12 int BFS()
    13 {
    14     que.push(n);
    15     step[n]=0;
    16     vis[n]=1;
    17     while(que.size())
    18     {
    19         int p=que.front();
    20         que.pop();
    21         if(p==k) break;
    22         for(int i=0;i<3;i++)
    23         {
    24             int dx;
    25             if(i==0) dx=p-1;
    26             if(i==1) dx=p+1;
    27             if(i==2) dx=2*p;
    28             if(dx>=0&&dx<=100000&&vis[dx]==0)
    29             {
    30                 que.push(dx);
    31                 step[dx]=step[p]+1;
    32                 vis[dx]=1;
    33             }
    34         
    35         }
    36     }
    37     return step[k]; 
    38 }
    39 
    40 int main()
    41 {
    42     while(cin>>n>>k)
    43     {
    44         memset(vis,0,sizeof(vis));
    45         memset(step,0,sizeof(step));
    46         ans=BFS();
    47         cout<<ans<<endl;
    48     }
    49     return 0;
    50  } 

     

  • 相关阅读:
    jQuery事件委托
    jQuery-事件面试题
    jQuery事件处理
    文档—CUD
    jQuery练习
    jQuery-筛选
    5. Longest Palindromic Substring
    340. Longest Substring with At Most K Distinct Characters
    159. Longest Substring with At Most Two Distinct Characters
    438. Find All Anagrams in a String
  • 原文地址:https://www.cnblogs.com/zjl192628928/p/9313363.html
Copyright © 2020-2023  润新知