POJ3278 HDU2717 Catch That Cow【BFS】

本文介绍了一道经典的算法题目——追牛问题。问题设定为在一条直线上,一个人从位置K出发,采用步行或瞬间移动的方式追赶位于位置N的静止不动的牛。目标是最小化到达目标位置所需的步数。文章提供了详细的BFS算法解决方案,并附带了完整的C++代码实现。

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

Catch That Cow

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 100475 Accepted: 31438

Description

Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a pointN (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 orX + 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 andK

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.

Source

USACO 2007 Open Silver

 

问题链接POJ3278 HDU2717 Catch That Cow

题意简述

  一条线上,人的FJ的起点为K位置,牛在N位置(牛不动),输入正整数K和N。若FJ在x位置,FJ有三种走法,分别是走到x-1、x+1或2x位置。求从K走到N的最少步数。

问题分析

  典型的BFS问题。在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少。

程序说明

  程序中,使用了一个队列来存放中间节点。

  需要说明的是,除了BFS方法,这个题应该可以用分支限界法来解,需要更高的技巧。

 

AC的C++语言程序如下:

 

/* POJ3278 HDU2717 Catch That Cow */

#include <iostream>
#include <cstring>
#include <queue>

using namespace std;

const int MAXN = 100000;
const int MAXN2 = MAXN * 2;

bool notvist[MAXN * 2 + 2];

int n, k, ans;

struct node {
    int p, level;
};

node start;

void bfs()
{
    queue<node> q;
    int nextp;

    memset(notvist, true, sizeof(notvist));

    notvist[n] = false;
    start.p = n;
    start.level = 0;

    ans = 0;
    q.push(start);

    while(!q.empty()) {
        node front = q.front();
        q.pop();

        if(front.p == k) {
            ans = front.level;
            break;
        }

        nextp = front.p - 1;        /* x-1 */
        if(nextp >= 0 && notvist[nextp]) {
            notvist[nextp] = false;

            node v;
            v.p = nextp;
            v.level = front.level + 1;
            q.push(v);
        }

        nextp = front.p + 1;        /* x+1 */
        if(nextp <= MAXN2 && notvist[nextp]) {
            notvist[nextp] = false;

            node v;
            v.p = nextp;
            v.level = front.level + 1;
            q.push(v);
        }

        nextp = front.p + front.p;      /* 2x */
        if(nextp <= MAXN2 && notvist[nextp]) {
            notvist[nextp] = false;

            node v;
            v.p = nextp;
            v.level = front.level + 1;
            q.push(v);
        }
    }
}

int main()
{
    while(cin >> n >> k) {
        bfs();

        printf("%d\n", ans);
    }

    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值