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 - 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.

三个搜索方向 +1, -1, *2  用bfs搜索 数组要开到最大值的2倍 注意剪枝和边界问题

#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
using namespace std; const int Max = ;
int s, e;
struct node
{
int x, step;
}now, Next;
bool v[];
queue<node>q; int bfs()
{
now.x = s;
now.step = ;
v[s] = ;
q.push(now);
while (!q.empty()) {
now = q.front();
q.pop();
if (now.x == e)
return now.step; Next.x = now.x*;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next);
v[Next.x] = ;
} Next.x = now.x-;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next); //若先检查v[Next.x] 会出现数组越界v[-1]的情况
v[Next.x] = ;
} Next.x = now.x+;
Next.step = now.step+;
if (Next.x >= && Next.x <= Max && !v[Next.x]){
q.push(Next);
v[Next.x] = ;
} }
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
cin >> s >> e;
memset(v, , sizeof(v));
cout << bfs(); return ;
}

[poj] Catch That Cow--bfs的更多相关文章

  1. 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,最先 ...

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

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

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

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

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

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

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

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

  6. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  7. POJ3278 Catch That Cow —— BFS

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

  8. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  9. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  10. poj 3278 Catch That Cow bfs

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

随机推荐

  1. Error: Failed to fetch plugin E:_My_File______workMyCodemyCodecordova-workspaceplugman-testMyMath via registry. Probably this is either a connection problem, or plugin spec is incorrect.

    $ cordova plugin add E:\_My_File_____\_work\MyCode\myCode\cordova-workspace\plugman-test\MyMath --sa ...

  2. PHP通过session id 实现session共享和登录验证的代码

    先说说,这个机制的用途吧,到现在为止战地知道这个机制有两个方面的用途: 首先,多服务器共享session问题,这个大家应该都能够理解的,当一个网站的用户量过大,就会使用服务器集群,例如专门有一个登录用 ...

  3. SAP采购寄售业务操作步骤

    [转自 http://blog.sina.com.cn/s/blog_6466e5f70100jghg.html] 这里所示的是比较完整的步骤,包含了:信息记录.采购合同.货源清单.采购申请.采购订单 ...

  4. 磁卡ID卡IC卡的区别【转】

    本文转载自:https://blog.csdn.net/trap94/article/details/50614451 今天被一个朋友问到ID卡和IC卡有什么区别,还真给问住了.虽然平时经常用到这些卡 ...

  5. Tomcat实现多主多备

    Nginx Upstream 实现简单双机主从热备 下面配置多主多从: upstream testproxy { server 127.0.0.1:8080; server 127.0.0.1:808 ...

  6. unix下面是常用命令及简单说明

    摘自:http://blog.sina.com.cn/s/blog_629b80af01018k7x.html 命令 功能简述 acctcom 等于进程记帐文件 accton 启动或中止记帐进程 ad ...

  7. 写xml时候的一个坑

    <DOCTYPE scores[]>这一行总是显示错误,折腾了一晚上,后来无意错误在于:<!ELEMENT scores(student+)>应该写成:<!ELEMENT ...

  8. 分享知识-快乐自己:Hibernate 中的 HQL 语句的实际应用

    概要: Hibernate 支持三种查询方式: HQL查询.Criteria查询及原声 SQL (Native SQL)查询. HQL(Hibernate Query Language,Hiberna ...

  9. python基础-文本操作

    文件IO #文件的基本操作 1.在python中你可以用file对象做大部分的文件操作 2.一般步骤: 先用python内置的open()函数打开一个文件,并创建一个file对象, 然后调用相关方法进 ...

  10. HTml js 生成图片

    <script type="text/javascript"> function $(id) { return document.getElementById(id); ...