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 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.
 
 
开始考虑用dfs做。。没考虑双向问题,向左向右会陷入死循环。还是BFS大法好,,求最短路径问题,遍历部分点即可。
 
 
#include<stdio.h>
#include<queue>
using namespace std; struct Node{
int x,y;
}node;
int b[]; int main()
{
int n,k;
queue<Node> q;
scanf("%d%d",&n,&k);
if(n==k) printf("0\n"); //特判
else{
node.x=n;
node.y=;
q.push(node);
b[n]=;
while(q.size()){
node.x=q.front().x*;
node.y=q.front().y+;
if(node.x<=&&node.x>=){
if(node.x==k){
printf("%d\n",node.y);
break;
}
if(b[node.x]==){
b[node.x]=;
q.push(node);
}
}
node.x=q.front().x+;
node.y=q.front().y+;
if(node.x<=&&node.x>=){
if(node.x==k){
printf("%d\n",node.y);
break;
}
if(b[node.x]==){
b[node.x]=;
q.push(node);
}
}
node.x=q.front().x-;
node.y=q.front().y+;
if(node.x<=&&node.x>=){
if(node.x==k){
printf("%d\n",node.y);
break;
}
if(b[node.x]==){
b[node.x]=;
q.push(node);
}
}
q.pop();
}
}
return ;
}

POJ - 3278 Catch That Cow BFS求线性双向最短路径的更多相关文章

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

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

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

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

  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(基础水)

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

  5. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

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

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

  8. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  9. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

随机推荐

  1. Django1.11.4中文文档

    Django管理站点¶ 自动管理界面是Django最强大的部分之一.它从您的模型中读取元数据,以提供一个快速,以模型为中心的界面,让受信任的用户可以管理您网站上的内容.管理员建议的使用仅限于组织的内部 ...

  2. ComboBox在WPF中的绑定示例:绑定项、集合、转换,及其源代码

    示例1.Selector(基类) 的示例Controls/SelectionControl/SelectorDemo.xaml <Page x:Class="Windows10.Con ...

  3. RabbitMQ 内存和磁盘警告

    内存和磁盘警告 有两种情况下,Rabbit MQ会停止读取客户端的连接socket,为了防止crash崩溃,这两种情况是: 内存使用达到配置值 磁盘空间下降到配置值下 在这两种情况下,服务器会临时阻塞 ...

  4. 基于注解的Sping AOP详解

    一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...

  5. Angular中的内置指令和自定义指令

    NG中的指令,到底是什么(what)? 为什么会有(why)?以及怎样使用(how)? What: 在NG中,指令扩展HTML功能,为 DOM 元素调用方法.定义行为绑定数据等. Why: 最大程度减 ...

  6. 配置tomcat,访问端口改为80

    首先:找到tomcat的的config文件夹下的server.xml文件: 编辑server.xml 保存server.xml文件,重启tomcat服务器,即可. 亲测好使.

  7. Linux集群基础

    Linux集群基础 作者:Danbo 时间:2015-7-12 集群概述 什么是集群?集群是一组协同工作的服务器实体.用以提供比单一服务实体更具扩展性和可用性的平台. 集群的分类 1.HPC(High ...

  8. linux命令行快捷键记录

    摘自: http://www.cnblogs.com/webzhangnan/p/3221410.html [移动光标] Ctrl+A 标移到行首.它在多数文本编辑器和 Mozilla 的 URL 字 ...

  9. AndroidManifest配置之uses-sdk

    uses-sdk配置 uses-sdk用来设置app对android系统的兼容性.它包含三个可选的配置项,分别为android:minSdkVersion,android:targetSdkVersi ...

  10. YII的RBAC

    转自:http://www.cppblog.com/guojingjia2006/archive/2013/01/15/197298.html 开始准备 Yii提供了强大的配置机制和很多现成的类库.在 ...