HDU:Catch that cow

本文介绍了一个有趣的算法问题——如何利用广度优先搜索找到固定不动的奶牛。通过定义结构体来记录位置和时间,实现农夫在数轴上的移动策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11968    Accepted Submission(s): 3738


Problem 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
题目大意:
就是有一个农夫和一头奶牛都在一个数轴上,这只奶牛自始至终都不会动,农夫在数轴上的行走规律是可以向左走一步,向右走一步,或跳到是当前坐标位置的2倍的地方,而且这三种行走情况所消耗的时间都是一秒,求农夫抓到奶牛所用的最短时间。
题目思路:
用广搜加队列,定义一个结构体,其所包含的成员是在数轴上的坐标,以及农夫走到这个位置所需要的时间,再输入的时候我们先把农夫的起始位置标注出来,将横坐标的值赋给结构体变量的横坐标,将时间赋为0,然后让这个结构体变量进队列,建立一个while循环当队列非空的时候一直执行循环体,没执行一次循环让cur为队首元素,即农夫的当前位置,我们要先看一下这个位置是不是已经是奶牛所在的位置了,如果当前位置是奶牛所在的位置了,那么说明农夫已经抓到奶牛了,直接返回农夫从起始位置走到当前位置所化肥及的时间,这就是最短时间,如果当前位置不是奶牛的位置,然后执行三种情况,下一位置next可以是cur.x+1,cur.x-1,cur.x*2,执行这三种情况我们都要让农夫到达下一位置的时间加1,然后我们让这三种情况所得到的next直接进队,
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int farmerpos,cowpos;     //起始农夫位置,起始奶牛位置
int sign[1000010];
typedef struct
{
    int pos;          //位置坐标
    int time;         //农夫到达该位置的时间
}position;
int bfs()
{
    queue<position>Q;    //定义一个类型为结构体变量的队列,名字叫做Q
    position temp,cur,next;      //cur为当前位置,next为下一位置
    temp.pos=farmerpos;        //起始位置为农夫的坐标
    temp.time=0;                     //初始时间为0
    sign[farmerpos]=0;            //初始位置赋为0,代表这个位置已经到过了
    Q.push(temp);                   //入队
    while(!Q.empty())              //队列非空,一直循环下去
    {
        cur=Q.front();        //取队首元素
        Q.pop();              //让队首元素出队
        if(cur.pos==cowpos)    //如果当前坐标等于牛的坐标
            return cur.time;    //则输出到达这个位置的时间
        next.pos=cur.pos+1;      //否则进行三种情况讨论,并让着三种选择所得到的结果都进入队列
        if(sign[next.pos]&&next.pos>=0&&next.pos<=1000000)        //这个位置没有到过,且这个位置是合法的
        {
            next.time=cur.time+1;              //到达下一位置的时间+1
            sign[next.pos]=0;                      //这个位置标记为0,代表这个位置已经到过了
            Q.push(next);                           //让next入队
        }                //后面的同理
        next.pos=cur.pos-1;
        if(sign[next.pos]&&next.pos>=0&&next.pos<=1000000)
        {
            next.time=cur.time+1;
            sign[next.pos]=0;
            Q.push(next);
        }
        next.pos=cur.pos*2;
        if(sign[next.pos]&&next.pos>=0&&next.pos<=100010)
        {
            next.time=cur.time+1;
            sign[next.pos]=0;
            Q.push(next);
        }
    }
    return -1;
}
int main()
{
    int ans;
    while(~scanf("%d%d",&farmerpos,&cowpos))
    {
        memset(sign,1,sizeof(sign));
        ans=bfs();
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值