• 暑期第一弹<搜索> C


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

    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.


    题意:有一个农夫在N位置,他得牛在K位置。他每一步可以向左走一步,向右走一步,走到当前位置的2倍处。问农夫碰到牛所需的最短步骤。

    思路:BFS三个状态,初始为N的位置。边界判断!!!可行判断!!!

    代码如下:

     

    #include <iostream>
    #include <queue>
    #include <cstring>
    using namespace std;
    
    struct Node{
        int id;
        int cont;
    };
    int n,k;
    int visit[200005];      //虽然没看到题意说每个位置只能走一次,但是不标记的话会内存超限
    
    void bfs(){
        queue <Node> Q;
        while(!Q.empty())
            Q.pop();
        Node h;
        h.id = n;
        h.cont = 0;
        visit[n] = 1;
        Q.push(h);
        while(!Q.empty()){
            Node res = Q.front();
            Q.pop();
            if(res.id == k){
                cout<<res.cont<<endl;
                break;
            }
    
            int ID = res.id;
            int Cont = res.cont;
            if(ID <= k && !visit[ID+1]){        //可行判断
                visit[ID+1] = 1;
                Node temp;
                temp.id = ID+1;
                temp.cont = Cont+1;
                Q.push(temp);
            }
            if(ID >= 1 && !visit[ID-1]){        //可行判断
                visit[ID-1] = 1;
                Node temp;
                temp.id = ID-1;
                temp.cont = Cont+1;
                Q.push(temp);
            }
            if(ID <= k && !visit[ID*2]){        //可行判断
                visit[ID*2] = 1;
                Node temp;
                temp.id = ID*2;
                temp.cont = Cont+1;
                Q.push(temp);
            }
        }
    }
    
    int main()
    {
        while(cin>>n>>k){
            memset(visit,0,sizeof(visit));
            bfs();
        }
        return 0;
    }
  • 相关阅读:
    深度可分离卷积、分组卷积、空洞卷积、转置卷积
    Batch Normalization
    激活函数
    容器————vector
    39XML文档类
    38初识xml
    37QT程序打包
    36可视化操作数据库
    35使用模型操作数据库
    34sqlite
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351941.html
Copyright © 2020-2023  润新知