zoukankan      html  css  js  c++  java
  • 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L
    来源:牛客网

    Catch That Cow
    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 32768K,其他语言65536K
    64bit IO Format: %lld
    题目描述
    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?
    输入描述:
    Line 1: Two space-separated integers: N and K
    输出描述:
    Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.
    示例1
    输入
    复制
    5 17
    输出
    复制
    4
    说明
    Farmer John starts at point 5 and the fugitive cow is at point 17.
    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.

    题意:
    给你一个位置n,一个位置k,都在水平的坐标轴上,你每一次可以向左或者向右走一步,或者从当先的x移动到2*x 位置,问你从n到k最小需要多少步?
    思路:
    直接BFS搜,用一个数组vis 维护 每一个位置是否被访问,由于BFS的性质,一个位置如果已经被访问过了,就不需要再访问了,因为当前的步数一定大于等于访问时候的步数。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define dll(x) scanf("%I64d",&x)
    #define xll(x) printf("%I64d
    ",x)
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
    inline void getInt(int* p);
    const int maxn = 2000010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    bool vis[maxn];
    queue<pii> q;
    int main()
    {
        //freopen("D:\code\text\input.txt","r",stdin);
        //freopen("D:\code\text\output.txt","w",stdout);
        int n,k;
        cin>>n>>k;
        vis[n]=1;
        q.push(mp(n,0));
        pii t;
        while(!q.empty())
        {
            t=q.front();
            q.pop();
            if(t.fi==k)
            {
                cout<<t.se<<endl;
                break;
            }else
            {
                if(t.fi+1<maxn&&!vis[t.fi+1])
                {
                    vis[t.fi+1]=1;
                    q.push(mp(t.fi+1,t.se+1));
                }
                if(t.fi-1>=0&&!vis[t.fi-1])
                {
                    vis[t.fi-1]=1;
                    q.push(mp(t.fi-1,t.se+1));
                }
                if(t.fi*2<maxn&&!vis[t.fi*2])
                {
                    vis[t.fi*2]=1;
                    q.push(mp(t.fi*2,t.se+1));
                }
            }
        }
        return 0;
    }
     
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
     
     
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    面向对象编程,其属性特性,用法等
    re正则模块细解,面向对象编程思路的优劣。
    机器人学——1.2-三维空间位姿描述
    机器人学——1.1-二维空间位姿描述
    机器人学——1.0-位置与姿态概述
    latex教程:1.2-latex现状
    latex教程: 1.1-历史
    windows安装opencv
    使用pip安装Opencv
    在Ubuntu上安装opencv-python
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11253060.html
Copyright © 2011-2022 走看看