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

    Catch That Cow
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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 - 1 or + 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.


    简单题,不说,直接代码。。。

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<map>
    #include<set>
    #include<ctime>
    #define eps 1e-6
    #define MAX 100005
    #define INF 0x3f3f3f3f
    #define LL long long
    #define pii pair<int,int>
    #define rd(x) scanf("%d",&x)
    #define rd2(x,y) scanf("%d%d",&x,&y)
    #define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
    ///map<int,int>mmap;
    ///map<int,int >::iterator it;
    using namespace std;
    
    struct Pos
    {
        int x;
        int step;
        Pos() {}
        Pos(int x,int step)
        {
            this->x=x,this->step=step;
        }
    };
    bool vis[MAX];
    int main ()
    {
        int m,n;
        while(~rd2(m,n))
        {
            memset(vis,0,sizeof(vis));
            if(m>n)
            {
                printf("%d
    ",m-n);
                continue;
            }
            queue<Pos> que ;
            que.push(Pos(m,0));
            while(!que.empty())
            {
                Pos temp=que.front();
                if(temp.x==n)
                {
                    printf("%d
    ",temp.step);
                    break;
                }
                que.pop();
                if( temp.x-1>=0 && !vis[temp.x-1] )
                {
                    que.push(Pos(temp.x-1,temp.step+1));
                    vis[temp.x-1]=true;
                }
                if( temp.x+1<=MAX-5 && !vis[temp.x+1] )
                {
                    que.push(Pos(temp.x+1,temp.step+1));
                    vis[temp.x+1]=true;
                }
                if( temp.x<<1<=MAX-5 && !vis[temp.x<<1])
                {
                    que.push(Pos(temp.x*2,temp.step+1));
                    vis[temp.x<<1]=true;
                }
            }
        }
        return 0;
    }
    




  • 相关阅读:
    如何优化软件测试成本?——转译
    Pytest(下篇)
    人与机器人如何协同来测试软件——译
    Appium自动化测试遇到的chromedriver/chrome坑
    Pytest(上篇)
    【selenium】利用selenium进行爬虫时,防止js检测驱动的方法
    【python】python分词和识别图像中的文字
    【python】Python获取QQ截图热键(ctrl+alt+a)截取的内容
    【python】pyinstaller如何将自己写的模块一并打包到exe中
    【python】python启动线程查看线程的名称和id;python杀掉进程的方法
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717990.html
Copyright © 2011-2022 走看看