• POJ4001(3入口のBFS)


    4001:Catch That Cow

    时间限制:
    2000ms
    内存限制:
    65536kB
    描述

    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.
    样例输入
    5 17
    样例输出
    4
    提示
    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.

    Tips:

    大致题意:

    给定两个整数n和k

    通过 n+1或n-1 或n*2 这3种操作,使得n==k

    输出最少的操作次数

    #include"iostream"

    #include"queue"

    #include"cstring"

    using namespace std;

    bool vis[3*100000];

    class pos{

    public:

             int step;

             int x;

    };

    int main(){

             queue<pos> q;

             int n,k;

             while(cin>>n>>k){

                       memset(vis,false,sizeof(vis));

                       pos t1,t2;

                       t1.step=0;

                       t1.x=n;

                       q.push(t1);

                       vis[t1.x]=true;

                       while(!q.empty()){

                       t1=q.front();

                       q.pop();

                       if(t1.x==k){

                                cout<<t1.step<<endl;

                                break;}

                       if(t1.x-1>=0&&!vis[t1.x-1//t1.x-1>=0是剪枝操作,位置可以为0

                                t2.x=t1.x-1;

                                t2.step=t1.step+1;

                                q.push(t2);

                                vis[t1.x-1]=true;

                       }

                       if(t1.x+1<=2*k&&!vis[t1.x+1]){//t1.x+1<=2*k是剪枝操作,范围放大到2*k

                                t2.x=t1.x+1;

                                t2.step=t1.step+1;

                                q.push(t2);

                                vis[t1.x+1]=true;

                       }

                       if(2*t1.x<=2*k&&!vis[2*t1.x]){//2*t1.x<=2*k是剪枝操作,范围放大到2*k

                                t2.x=2*t1.x;

                                t2.step=t1.step+1;

                                q.push(t2);

                                vis[2*t1.x]=true;

                       }

                       }

             }

    }

  • 相关阅读:
    081、Weave Scope 多主机监控(2019-04-29 周一)
    080、Weave Scope 容器地图(2019-04-28 周日)
    079、监控利器 sysdig (2019-04-26 周五)
    078、Docker 最常用的监控方案(2019-04-25 周四)
    077、跨主机使用Rex-Ray volume (2019-04-24 周三)
    076、创建Rex-Ray volume (2019-04-23 周二)
    075、配置Virtualbox backend(2019-04-22 周一)
    074、如何安装和配置Rex-Ray?(2019-04-19 周五)
    073、如何实现跨Docker Host 存储? (2019-04-18 周四)
    072、一文搞懂各种Docker网络 (2019-04-17 周三)
  • 原文地址:https://www.cnblogs.com/lzhitian/p/2342386.html
Copyright © 2020-2023  润新知