• C


    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.
     
    BFS+剪枝:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <xfunctional>
    using namespace std;
    int dir[6][3] = { { 0,0,1 },{ 0,0,-1 },{ 0,-1,0 },{ 0,1,0 },{1,0,0},{-1,0,0} };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    #define ll  long long;
    #define PII  pair<int, int>;
    const int mod = 1e9 + 7;
    const int marown = 1000 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    struct node
    {
        int pos,cnt;
    };
    int main()
    {
        int n, k;
        cin >> n >> k;
        queue<node> que;
        vector<bool> visited(100001, false);
        if (k <= n)
        {
            cout << n - k;
        }
        else
        {
            node tmp;
            tmp.cnt = 0;
            tmp.pos = n;
            que.push(tmp);
            visited[n] = true;
            while (!que.empty())
            {
                node front = que.front();
    
                if (front.pos == k)
                {
                    cout << front.cnt;
                    break;
                }
                node tmpadd, tmpsub, tmp2;
                tmpadd.pos = front.pos + 1;
                tmpsub.pos = front.pos - 1;
                tmp2.pos = front.pos * 2;
                tmpadd.cnt = tmpsub.cnt = tmp2.cnt = front.cnt + 1;
                if (tmpadd.pos > 0 && tmpadd.pos <= 100000 && visited[tmpadd.pos]==false)
                {
                    visited[tmpadd.pos] = true;
                    que.push(tmpadd);
                }
                if (tmpsub.pos > 0 && tmpsub.pos <= 100000 && visited[tmpsub.pos] == false)
                {
                    visited[tmpsub.pos] = true;
                    que.push(tmpsub);
                }
                if (tmp2.pos > 0 && tmp2.pos <= 100000 && visited[tmp2.pos] == false)
                {
                    visited[tmp2.pos] = true;
                    que.push(tmp2);
                }
                que.pop();
            }
        }
        return 0;
    }
     
  • 相关阅读:
    程序员式的幽默(灌水)
    你应该知道的
    WPF控件应用[0]
    WPF控件应用[2]
    C#调用Win32 的API函数User32.dll
    C#获取当前行号
    C#导入excel重写
    [转]wpf相关好资源
    使用C#和Excel进行报表开发-生成统计图Chart
    [转]用 delphi 创建一个服务程序
  • 原文地址:https://www.cnblogs.com/dealer/p/12550568.html
Copyright © 2020-2023  润新知