zoukankan      html  css  js  c++  java
  • 暑期第一弹<搜索> 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;
    }
  • 相关阅读:
    呵呵,23号了,难道要通宵???
    开会效果不错
    开完会罗,明天离开这上有点奢华的地方:)
    感冒了,感觉糟透了。。。
    你好,深圳!你好,2010!
    上班 第一天
    。。轻。。。
    那天我生日
    英雄七十寿 无物下冷酒 横刀上闹市 直取数人头
    李孝利 唠叨
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6351941.html
Copyright © 2011-2022 走看看