zoukankan      html  css  js  c++  java
  • BFS --- 模板题

    Catch That Cow
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 36079   Accepted: 11123

    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 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?

    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.

    【题目来源】

    【题目大意】
    在x轴的正半轴上,一个人的出发点是N,一头牛在另一点K,人可以有两种操作:
    1.步行:+1 or -1
    2.传送:*2
    问你通过最少的步数到达牛的位置,需要多少步。
     
    【题目分析】
    就是一个裸的广搜,使用队列实现。
     
    #include<iostream>
    #include<cstring>
    #include<queue>
    using namespace std;
    int N,K;
    
    struct now
    {
        int x;
        int step;
    };
    bool vis[1000010];
    
    int BFS(int x)
    {
        int tx;
        now ans,tp;
        queue<now> que;
        ans.x=x,ans.step=0;
        vis[x]=true;
        que.push(ans);
        while(!que.empty())
        {
            tp=que.front();
            que.pop();
            tx=tp.x+1;   //no.1
            if(tx<0||tx>1000000);
            else
            {
                if(!vis[tx])
                {
                    vis[tx]=true;
                    ans.x=tx,ans.step=tp.step+1,que.push(ans);
                    if(ans.x==K) return ans.step;
                }
            }
            tx=tp.x-1;    //no.2
            if(tx<0||tx>1000000);
            else
            {
                if(!vis[tx])
                {
                    vis[tx]=true;
                    ans.x=tx,ans.step=tp.step+1,que.push(ans);
                    if(ans.x==K) return ans.step;
                }
            }
            tx=tp.x*2;   //no.3
            if(tx<0||tx>1000000);
            else
            {
                if(!vis[tx])
                {
                    vis[tx]=true;
                    ans.x=tx,ans.step=tp.step+1,que.push(ans);
                    if(ans.x==K) return ans.step;
                }
            }
        }
    }
    
    int main()
    {
        while(cin>>N>>K)
        {
            if(N==K)
            {
                cout<<"0"<<endl;
                continue;
            }
            memset(vis,false,sizeof(vis));
            cout<<BFS(N)<<endl;
        }
        return 0;
    }

    当然还可以剪枝一下,比如说:如果人的坐标大于牛的坐标,这时就只需要做-1这一步就可以了。

  • 相关阅读:
    0diff算法参考资料
    js 对象属性值
    一些带有设计模式的优秀代码
    vue 配置多页面
    cms 管理系统
    网络技术:EtherChannel 链路汇聚
    网络技术:VLAN 中继协议(VTP)
    网络管理:管理信息库
    网络管理:抽象语法表示 ASN.1
    网络管理:基本编码规则(BER)
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3748504.html
Copyright © 2011-2022 走看看