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. Ubuntu引导出问题grub rescu模式下:“error : unknown filesystem”或者 找不到normal.mod 的解决办法

    感谢http://www.linuxidc.com/Linux/2012-06/61983.htm,因为参考了其中的内容. 下面是修改和完善. 问题原因: (win7,ubuntu双系统下) 强制关机 ...

  2. Canvas学习笔记——动画环境中的边界

    在动画中经常要处理边界问题,比如一个物体运动到了边界,要怎么处理才合适呢?通常有几种以下几种方式: 让物体消失 // > 16 & 0xff, g = color >> 8 ...

  3. 基于TCP的通信程序设计

    套接字(Socket)是一种跨主机进程之间的双向通信接口,每个打开的套接字都可以通过一个套接字描述符来描述,因此可以使用低级文件编程库操作套接字. TCP是一中面向连接的网络传输控制协议.它每发送一个 ...

  4. Android中通过GPS或NetWork获取当前位置的经纬度

    今天在Android项目中要实现一个通过GPS或NetWork来获取当前移动终端设备的经纬度功能.要实现该功能要用到Android Framework 中的 LocationManager 类.下面我 ...

  5. 基于XML配置的Sping AOP详解

    一.编写基本处理方法 package com.kang.sping.xml.aop; public class Math{ //加 public int add(int n1,int n2){ int ...

  6. js 数据获取

    http://www.cnhan.com/shantui/templates/MC500/TP001/js/template.js var qiaoContent="0";//0默 ...

  7. accessor mothod mutator mothod 更改器方法 访问器方法 类的方法可以访问类的任何一个对象的私有域!

    LocalDate.plusDate String.toUpperCase GregorianCalendar.add import java.time.*; public class Calenda ...

  8. LESS和sa

    一. Sass/Scss.Less是什么? Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,Sass语法属于缩排语法,比css比多出好些功能(如变量 ...

  9. Activity和ListActivity的区别

    http://book.51cto.com/art/201007/212051.htm

  10. java使用ftp局域网内多线程上传图片过慢

    多线程ftp上传文件时候,图片上传很慢,调试和查询资料发现主要在:storeFile方法 解决方案如下: FTPClient fc设置setBufferSize 可以根据内存大小适当设置大点的缓冲区: ...