Catch That Cow(广度优先搜索_bfs)



Catch That Cow

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 48036   Accepted: 15057

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

题意:输入两个数n,k。求从n到k最少走多少步。可以前进1后退1或者当前的位置*2;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct node
{
    int x;//当前位置
    int ans;//走的步数
}q[1000010];
int vis[1000010];//标记变量,该点是否被访问;
int jx[]={-1,1};//后退1或者前进1;
struct node t,f;
int n,k;
void bfs()
{
     int i;
     int s=0,e=0;//指针模拟队列。往队列加e++ 往队列里提出数s++
     memset(vis,0,sizeof(vis));
     t.x=n;//当前初始位置
     vis[t.x]=1;//标记为1代表访问过;
     t.ans=0;//初始位置步数为0;
     q[e++]=t;//把当前步数加人队列
     while(s<e)//当队列不为空
    {
        t=q[s++];//提出
        if(t.x==k)//如果该数正好等于目标位置直接输出步数
        {
             printf("%d\n",t.ans);
             break;
        }
        for(i=0;i<3;i++)//i=0后退一步,i=1前进一步,i=2此时的位置*2;
        {
             if(i==2)
             {
                 f.x=t.x*2;
             }
             else
             {
                 f.x=t.x+jx[i];
             }
             if(f.x>=0&&f.x<=100000&&!vis[f.x])
             {
                 f.ans=t.ans+1;
                 q[e++]=f;
                 vis[f.x]=1;
             }
        }
    }
}
int main()
{
    while(~scanf("%d %d",&n,&k))
    {
        bfs();
    }
    return 0;
}

时间: 2024-10-29 19:10:56

Catch That Cow(广度优先搜索_bfs)的相关文章

poj 3278 Catch That Cow (bfs搜索)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 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,00

POJ - 3278 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 m

Catch That Cow抓住那只奶牛(BFS,广度优先搜索)

# **Catch That Cow(抓住那只奶牛)**[题意]:农场主要去追一直逃跑的牛,每次移动有三种选择,位置加一,位置减一,位置乘二,要找到最快的方法让农夫追到牛(也就是移动的次数最少,这个过程是线性的)具体的题目请见:[原题链接:](http://poj.org/problem?id=3278).思路: 从题目中位置移动就可以看出是一个搜索问题,又加上要找最快的方法,也就是在暗示这个是一个广搜题,每次搜索有三个方向,是一个常规题,不过也有细节,请跟随笔者我思路. 代码: #includ

Catch The Caw——(广度优先搜索的应用,队列)

抓住那头牛(POJ3278)农夫知道一头牛的位置,想要抓住它.农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000).农夫有两种移动方式:1.从X移动到X-1或X+1,每次移动花费一分钟2.从X移动到2*X,每次移动花费一分钟假设牛没有意识到农夫的行动,站在原地不动.农夫最少要花多少时间才能抓住牛? 广搜算法?广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败

hdu 2717 Catch That Cow

---恢复内容开始--- Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14433    Accepted Submission(s): 4396 Problem Description Farmer John has been informed of the location of a fugitive

POJ 3287 (基础BFS) Catch That Cow

这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节点取出放入Closed表,并记该节点为n:(4) 考察节点n是否为目标节点.若是,则得到问题的解,成功退出:(5) 若节点n不可扩展,则转第(2)步:(6) 扩展节点n,将其不在Closed表和Open表中的子节点(判重)放入Open表的尾部,并为每一个子节点设置指向父节点的指针(或记录节点的层次)

hdu 2717 Catch That Cow(广搜bfs)

题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7909    Accepted Submission(s): 2498 Problem Description Farmer John has been inform

HDU 2717 Catch That Cow (bfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12615    Accepted Submission(s): 3902 Problem Description Farmer John has been

POJ 3278 Catch That Cow(BFS,板子题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 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,00