HDU2717CatchThatCow(BFS很容易理解的例题)

                                     Catch That Cow

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.

翻译版:

农夫约翰已被告知逃亡母牛的位置,并希望立即抓住她。 他从数字线上的N点(0≤N≤100,000)开始,并且母牛在同一数字线上的点K(0≤K≤100,000)。 农夫约翰有两种交通方式:步行和传送。 *步行:FJ可以在一分钟内从任意点X移动到X-1或X + 1点*传送:FJ可以在一分钟内从任意点X移动到2×X点。 如果母牛不知道它的追求,根本不动,那么农夫约翰需要多长时间才能找回它? 输入线1:两个以空格分隔的整数:N和K输出线1:Farmer John捕捉逃亡牛所需的最短时间(以分钟为单位)。

样本输入5 17;

样本输出4;

提示:Farmer John到达逃亡牛的最快方法是沿着以下路径移动:5-10-9-18-17,需要4分钟。

思路:

这个是BFS的基本简单题,意思就是农夫在位置N处,而母牛在位置K处,解决这道题首先要建立一个结构体关于农夫的位置和农夫总共走的步数。再定义一个检查的函数,当如果x越界或者是这个点已经走过的话,那么就返回0,反之返回1。最后定义一个bfs的函数,首先要将队列清空,然后做判断,如果当前农夫的位置和母牛的位置相同的时候,直接返回步数,不符合这个情况的话,开始n-1,n+1,和2*n的三种情况的分类讨论,但三种讨论的话里面的东西都是一样的。下面上代码:

实质上就是结构体队列的应用:

AC代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<stack>
#include <queue>
using namespace std;

const int N = 1000000;
int map[1000001];//走过的标记1.没走过的标记0
int n,k;//n是农夫的位置,k是母牛的位置 
struct node//定义一个结构体记录位置和步数的 
{
    int x,step;
};
//check函数结果如果是0的话,第一就是越界,第二个就是这个点已经走过了 
int check(int x)
{
    if(x<0 || x>=N || map[x])	
        return 0;
    return 1;
}

int bfs(int x)
{
    int i;
    queue<node> q;//把q队列化 
    node a,next;//把a,next作为结构体 
    a.x = x;//结构体a初始化
    a.step = 0;
    map[x] = 1;//刚开始的点标记为1 
    q.push(a);//把a加入到队列的末尾
    while(!q.empty())
    {
        a = q.front();//先把队首读取 
        q.pop();//弹出对首
        if(a.x == k)//如果农夫的位置和母牛的位置相同的话
            return a.step;//直接返回步数
        next = a;
        //每次都将三种状况加入队列之中
        //1.这个是+1的情况 
        next.x = a.x+1;
        if(check(next.x))//无论是什么情况下这几行代码都是一样的 
        {
            next.step = a.step+1;//这个就是步数+1 
            map[next.x] = 1;//表示已经走过了这个点
            q.push(next);//把next结构体加入队列
        }
        next.x = a.x-1;
        if(check(next.x))
        {
            next.step = a.step+1;
            map[next.x] = 1;
            q.push(next);
        }
        next.x = a.x*2;
        if(check(next.x))
        {
            next.step = a.step+1;
            map[next.x] = 1;
            q.push(next);
        }
    }
}
 
int main()
{
    int ans;//统计最后的步数
    while(~scanf("%d%d",&n,&k))//n指的是农夫的位置,k指的是母牛的位置 
    {
        memset(map,0,sizeof(map));//数组初始化
        ans = bfs(n);
        printf("%d\n",ans);
    }
    return 0;
}

这个代码其实就相当于一维的bfs题目,能够完全理解这道题的话,你可能bfs已经理解了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值