zoukankan      html  css  js  c++  java
  • POJ 3278(bfs)

    Catch That Cow
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 31637   Accepted: 9740

    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.
     1 //注意下标不可变为-1,两个数组均要开到20w,为此吃了好几个re 
     2 //大致题意:n可以执行加一减一乘二,求至少几步到k 
     3 #include <iostream>
     4 #include <queue>
     5 #include <cstring>
     6 #include <cstdlib>
     7 using namespace std;
     8 const int N = 100000;
     9 int a[200005];
    10 queue <int > q;
    11 int vis[N*2+5];
    12 void bfs(int n,int k)
    13 {
    14     vis[n] = true;
    15     q.push(n);
    16     while(!q.empty())
    17     {
    18         int head = q.front();
    19         q.pop();
    20         if(head==k)
    21         {
    22             return ;
    23         }
    24         else
    25         {//原来没加大于0的条件,结果re 
    26             if(!vis[head-1]&&head<=N&&head>=1)//禁止下标变为-1 
    27             {
    28                 vis[head-1] = true;
    29                 a[head-1]+=a[head]+1;
    30                 q.push(head-1);
    31             }
    32             if(!vis[head+1]&&head<N&&head>=0)
    33             {
    34                 vis[head+1] = true;
    35                 a[head+1]+=a[head]+1;
    36                 q.push(head+1);
    37             }
    38             if(!vis[head*2]&&head<=50000&&head>=0)
    39             {
    40                 vis[head*2] = true;
    41                 a[head*2]+=a[head]+1;
    42                 q.push(head*2);
    43             }
    44         }
    45     }
    46 }   
    47 int main()
    48 {
    49     int n,k;
    50     cin>>n>>k;
    51     memset(vis,false,sizeof(vis));
    52     memset(a,0,sizeof(a));
    53     while(!q.empty())
    54         q.pop();
    55     bfs(n,k);
    56     if(n>=k)
    57         cout<<n-k<<endl;
    58     else
    59         cout<<a[k]<<endl;
    60     system("pause");
    61     return 0;
    62 }
  • 相关阅读:
    织网的日子里——第一章:TCP时间获取之客户端和服务器端程序
    Hdu 4517 小小明系列故事——游戏的烦恼
    MongoDB安装配置
    【算法学习】线性时间排序计数排序、基数排序和桶排序详解与编程实现
    C++中宏的使用技巧
    每三小时热备shell脚本文件
    OSS音频编程实例
    MySQL的Illegal mix of collationsy异常原因和解决方法
    SQL用了Union后的排序问题
    union 时只能查出一个表中的信息,另一个表只能查出字段
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2647919.html
Copyright © 2011-2022 走看看