zoukankan      html  css  js  c++  java
  • Catch That Cow

    Catch That Cow

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 113   Accepted Submission(s) : 46
    Problem 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
     
     1 #include<stdio.h>
     2 #include<queue>
     3 #include<string.h>
     4 using namespace std;
     5 const int MAXN=100010;
     6 int visit[MAXN];
     7 int now[3];
     8 int N,K,step,t;
     9 void bfs(){
    10     queue<int>dl;
    11     dl.push(N);
    12     visit[N]=1;
    13     if(N!=K){
    14     while(!dl.empty()){
    15         t=dl.size();
    16         step++;
    17         while(t--){
    18             now[0]=dl.front()+1;
    19             now[1]=dl.front()-1;
    20             now[2]=dl.front()*2;
    21             dl.pop();
    22             for(int i=0;i<3;i++){
    23                 if(now[i]==K)return;
    24                 if(now[i]>0&&now[i]<=100000&&!visit[now[i]]){//醉了,少了个0,错了n次;;;;; 
    25                     visit[now[i]]=1;dl.push(now[i]);
    26                 }
    27             }
    28         }
    29     }
    30 }
    31 }
    32 int main(){
    33     while(~scanf("%d%d",&N,&K)){
    34         memset(visit,0,sizeof(visit));
    35         step=0;
    36         bfs();
    37         printf("%d
    ",step);
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    no.5.print sum
    0.1 hint crack
    no.4 抽奖测试
    no2.crossdomain.xml批量读取(待完善)
    no.1
    day7-读写分离
    day6-主从
    day5-备份
    day4-用户授权
    Day3-体系结构+查询+导入/出
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4709312.html
Copyright © 2011-2022 走看看