zoukankan      html  css  js  c++  java
  • 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  } 

     

  • 相关阅读:
    PHPsession实现用户登陆功能
    asp.net core mvc基于Redis实现分布式锁,C# WebApi接口防止高并发重复请求,分布式锁的接口幂等性实现
    OpenSSL 下载和私钥证书、CERTIFICATE证书生成
    Java byte[] 转C# byte[]
    如何在Etherscan.io 部署ETH以太坊智能合约 如何在15分钟内创建你的加密货币
    JavaScript 关于金额、数字验证的正则表达式
    web api .net C# mvc API返回XML文档的解析并取值
    C#和PHP加密结果一致的DES加密解密算法。php实现和c#一致的DES加密解密
    C#常用的图片处理方法-图片剪切、图片压缩、多图合并代码
    BitMap 图片格式与Base64Image格式互转方法
  • 原文地址:https://www.cnblogs.com/zjl192628928/p/9313363.html
Copyright © 2011-2022 走看看