Catch That Cow

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
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点,有个牛在K点。农夫有三种移动方式:1)向前一步 2)向后一步 3)从当所在位置X瞬间移动到2*X点

    输出最少进行多少步,农夫能找到牛。(大坑是农夫和牛的活动范围在[0,100000])

解题思路:

    bfs即可。注意农夫的可活动范围

ps:HUD 2717与这个题一样,但是是多组输入输出,请注意!

Code:

 #include<string>
#include<cstring>
#include<cstdio>
#include<queue>
#include<iostream>
using namespace std;
int dis[],vis[],N,K;
queue<int> q;
int bfs(int N,int K)
{
q.push(N);
dis[N]=,vis[N]=;
while (!q.empty())
{
int x[],i;
int front=q.front();
q.pop();
x[]=front-,x[]=front+,x[]=front*;
for (i=; i<=; i++)
{
if (x[i]>=&&x[i]<=&&!vis[x[i]])
{
q.push(x[i]),vis[x[i]]=,dis[x[i]]=dis[front]+;
if (x[i]==K) return dis[x[i]];
}
}
}
}
int main()
{
cin>>N>>K;
if (N==K) cout<<;
else
cout<<bfs(N,K);
return ;
}

POJ3278——Catch That Cow(BFS)的更多相关文章

  1. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  2. POJ3278 Catch That Cow(BFS)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  3. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  4. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  5. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  6. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  7. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

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

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

随机推荐

  1. Using OpenCV Java with Eclipse

    转自:http://docs.opencv.org/trunk/doc/tutorials/introduction/java_eclipse/java_eclipse.html Using Open ...

  2. linux下golang的配置

    linux下golang的配置 之前开发golang一直在windows下,今天在linux下试了一下 ,遇到一些梗,比如go 找不到 sync包.花了一小时全部解决,把过程记录一下. 安装 go 我 ...

  3. iOS 非ARC基本内存管理系列 3-循环retain和@class

    1.@class 使用场景:对于循环依赖关系来说,比方A类引用B类,同时B类也引用A类: 可以看出Person和Card互相引用,此时如果使用#import编译报错!因此当使用@class在两个类中相 ...

  4. webuploader上传插件

    一:官网 http://fex.baidu.com/webuploader/ 二:示例

  5. C#语言的Image和byte数组的互相转换

    /// <summary> /// 字节数组转换为图片 /// </summary> /// <param name="buffer">字节数组 ...

  6. 安卓项目中使用JSON引发的一个小错误 Multiple dex files define Lorg/apache/commons/collections/Buffer

    原因: 这里添加的jar包和android自带的jar产生了冲突

  7. T-SQL实例 函数结果设置为列别名

    本文分享一个T-SQL的例子,将自定义函数的结果作为别名列,是个不错的应用实例,有兴趣的朋友研究下. T-SQL实例,学习下将函数结果作为别名列的方法. 代码: view source print? ...

  8. ORA-12838: cannot read/modify an object after modifying it in parallel

    insert /*+ append */ into my_all_objects  select * from my_all_objects; select * from my_all_objects ...

  9. easy ui datagrid 增,删,改,查等基本操作

    如下图: ①列表信息图 ②添加信息图 ③修改信息图 html代码: <%@ Page Title="" Language="C#" MasterPageF ...

  10. python logging 日志轮转文件不删除问题

    前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...