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. CAF(C++ actor framework)(序列化之复杂类,分析 还有自己不懂的细思恐极函数实现)(三)

    这里应该是序列化的最后一篇.感觉自己写的不是很好,也一点点在学习.这次就不贴上代码了.代码在github上的announce5.cpp.代码简单,但是分析下去会有细思恐极的感觉! 先看一下几个函数是干 ...

  2. 实验一:点亮led

    一.先看原理图:

  3. WCF 初识(一)

    WCF的前世今生 在.NETFramework 2.0以及前版本中,微软发展了Web Service(SOAP with HTTP communication),.NET Remoting(TCP/H ...

  4. DTCMS自定义标签,获取所有栏目以及获得二级子栏目导航

    取得二级栏目 DTcms.Web.UI\Label\category.cs中 get_category_child_list 返回当前所有子栏目 DTcms.Web.UI\Label\category ...

  5. 短租app简析

    本人应聘某短租app产品经理时做的材料,贴出来请高手指教. 所有内容来自公开资料,不涉及商业秘密.

  6. Linux vi 中搜索关键字

    当你用vi打开一个文件后,因为文件太长,如何才能找到你所要查找的关键字呢? 在vi里可没有菜单-〉查找 不过没关系,可以在命令模式下敲斜杆( / )这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然 ...

  7. 【原创】一起学C++ 之->(箭头符号) ---------C++ primer plus(第6版)

    1.C++新手在指定结构成员时,不知道何时用.运算符,何时是用->运算符. 结论:如果结构标识符是结构名,则使用句点运算符:如果标识符是指向结构的指针,则使用箭头运算符. #include &l ...

  8. psutil--跨平台的进程管理

    原文地址:http://www.jianshu.com/p/64e265f663f6 Python处理Windows进程 psutil(Python system and process utilit ...

  9. 如何应用CLR线程池来管理多线程

        class Program     {         static void Main(string[] args)         {             int intWorkerT ...

  10. python学习笔记18(UliPad 初体验)

    在windows下安装配置Ulipad 由于UliPad 是由wxPython 开发的,所以,需要先安装wxPython . wxPython下载地址: http://www.wxpython.org ...