Catch That Cow (BFS广搜)

问题描述:

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?

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.

解题思路:

bfs,三个方向搜索,x=x+1,x=x-1,x=x*2,最先搜索到的就是用时最短的。

代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
using namespace std;
int n,k;
int a[100010];

struct node
{
    int x;
    int step;
}now,net;

int bfs(int x)
{
    queue<node> q;
    now.x=x;
    now.step=0;
    q.push(now);
    while(q.size())
    {
        now=q.front();
        q.pop();
        //三种情况
        if(now.x==k)return now.step;
        net.x=now.x+1;
        if(net.x>=0&&net.x<=100000&&a[net.x]==0)
        {
            a[net.x]=1;
            net.step=now.step+1;
            q.push(net);
        }
        net.x=now.x-1;
        if(net.x>=0&&net.x<=100000&&a[net.x]==0)
        {
            a[net.x]=1;
            net.step=now.step+1;
            q.push(net);
        }
        net.x=now.x*2;
        if(net.x>=0&&net.x<=100000&&a[net.x]==0)
        {
            a[net.x]=1;
            net.step=now.step+1;
            q.push(net);
        }
    }
    return -1;
}

int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        memset(a,0,sizeof(a));
        a[n]=1;
        int ans=bfs(n);
        printf("%d\n",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/boboyuzz/p/10489166.html

时间: 2024-10-10 04:57:18

Catch That Cow (BFS广搜)的相关文章

poj 3278 Catch That Cow(广搜)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 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

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 t

HDU2717 Catch That Cow 【广搜】

Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7147    Accepted Submission(s): 2254 Problem Description Farmer John has been informed of the location of a fugitive cow and wants

Catch That Cow 经典广搜

链接:http://poj.org/problem?id=3278 题目: 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 s

[ACM] hdu 2717 Catch That Cow (BFS)

Catch That Cow 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

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

hdu1240/poj2225 BFS广搜的再理解

原题地址 HDUOJ POJ 题目介绍 题意 这同样是一道搜索题,所不同的是要搜索的图是三维的而不是二维的.但这并没什么大的改变,只是增加了两个搜索的方向而已. 陷阱 要注意的地方是,所给出的起点终点的坐标是按照 列,行,层的顺序. 关于BFS 与DFS不同,BFS能保证所搜到的路径一定是最短路径,所以我们不需要维护一个多维(此处为3维)数组来记录访问到每一点的最小步数,只需要维护一个多维数组来标记是否走过就可以了.DFS中是要不停回溯来找最短路径的,但是BFS是不需要的.这是BFS本身的性质所

hdu 1195:Open the Lock(暴力BFS广搜)

mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方面的介绍为0,那就找mediaxyz请教请教吧,这些可都是经验,非常宝贵! 以下是与mediaxyz在QQ上聊天的记录,只有一部分,因为QQ把之前的谈话删除了,但基本上精髓都可这里了. mediaxyz 23:40:26你说的qsable是c->global_quality吧 Leon 23:40:

bfs广搜

bfs广搜 什么是广搜? 百度百科:个人觉得解释地并不好 个人理解:跟dfs一样暴力穷举每种状态,只不过不是"一搜到底",而是一层一层的搜索 它有什么好处? 容易理解 骗分利器 好写 它有什么弊端? 慢.毕竟是穷举每一种状态 如何实现? 算法流程图如下: #include <iostream> #include <cstdio> #include <queue> void bfs() { 初始化tmp,并压入队列; while(队列不为空) { 取出