BFS应用:POJ3278 抓牛

题目描述:

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?

输入: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

解题思路:

        农民有3种移动方式,但如果想向后移动就只有一种移动方式,也就是说明如果农民的位置在牛的位置之前或相同(N>=K)农民就只能采用一步步回退的方式;

        如果牛在农民前面则3种移动方式都能用的上,想求出移动的步数可以BFS的方式逐层扩散,直到遍历到牛的位置,遍历的层数就是最短的路径。

解题步骤:

        1、若N>=K,直接输出N-K;

        2、若N<K,用BFS,定义一个步数数组step,下标为结点的值,内容为该结点到N的步数;

        3、设出发顶点为N,step[N]为0,将其入队后开始遍历;若队头元素为u,三个邻接点为u+1、u-1、2*u,且邻接点的步数比结点的步数+1。

        4、有队头元素 = k时,输出此元素的步数。

代码实现:

#include <iostream>
using namespace std;
#include <queue>
#define maxn 100001

int n, k;
bool vis[maxn];
int step[maxn];

void BFS()
{
    int u = 0, v = 0;
    step[n] = 0;
    queue<int> q;
    q.push(n);
    vis[n] = true;
    while (!q.empty())
    {
        u = q.front();
        q.pop();
        if (u == k)
        {
            cout << step[u] << endl;
            return;
        }
        v = u - 1;
        if (v >= 0 && v <= 100000 && !vis[v])
        {
            step[v] = step[u] + 1;
            vis[v] = true;
            q.push(v);
        }
        v = u + 1;
        if (v >= 0 && v <= 100000 && !vis[v])
        {
            step[v] = step[u] + 1;
            vis[v] = true;
            q.push(v);
        }
        v = 2 * u;
        if(v >= 0 && v <= 100000 && !vis[v])
        {
            step[v] = step[u] + 1;
            vis[v] = true;
            q.push(v);
        }
    }
}

int main()
{
    cin >> n >> k;
    if (n >= k)
    {
        cout << n - k << endl;
    }
    else
    {
        BFS();
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可惜浅灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值