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.

 题意:FJ要抓奶牛,输入n(FJ的位置),k(奶牛的位置),求FJ抓到奶牛所需最少时间。

 FJ有三种走法:

    1:向前移动一步,花费一分钟

    2:向后退后一步,花费一分钟

     3:向前移动当前位置的2倍,花费一分钟

 #include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int MAX=;
bool vis[MAX];
int step[MAX];
queue <int >q;
int bfs(int n,int k)
{
int head,next;
q.push(n);
step[n]=;
vis[n]=true;
while(!q.empty())
{
head=q.front(); //取队首
q.pop();
for(int i=; i<; i++)
{
if(i==)
next=head-;
if(i==)
next=head+;
if(i==)
next=head*;
if(next<||next>=MAX) //排除出界情况
continue;
if(!vis[next])
{
q.push(next); //入队
step[next]=step[head]+;//步数加一
vis[next]=true; //标记访问
}
if(next==k)
return step[next];
} }
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(vis,false,sizeof(vis));
memset(step,,sizeof(step));
while(!q.empty())
q.pop();
if(n>=k)
cout<<n-k<<endl;
else
cout<<bfs(n,k)<<endl;
}
return ;
}

poj 3278 Catch That Cow(bfs+队列)的更多相关文章

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

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

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

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

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

  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 难度:1

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

  8. POJ - 3278 Catch That Cow bfs 线性

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

  9. BFS POJ 3278 Catch That Cow

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

随机推荐

  1. linux cp 和scp详解

    linux之cp/scp命令+scp命令详解   名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... dire ...

  2. CentOS 6、7 安装 Golang

    方法一:使用二进制文件安装 (推荐) 1.下载二进制文件: wget https://storage.googleapis.com/golang/go1.7.3.linux-amd64.tar.gz ...

  3. 关于document的节点;用Dom2创建节点;

    一.关于节点 1.节点树状图 document>documentElement>body>tagName 2.节点类型 元素节点(标签).文本节点(文本).属性节点(标签属性) 3. ...

  4. 让 div中的div垂直居中的方法!!同样是抄袭来的(*^__^*)

    同样 ,水平居中很简单,给子div设置margin:0px auto; 垂直居中也不难::给父div设置display:table-cell;vertical-align:middle; 重点是dis ...

  5. (转)Ext.onReady详解

    (转自)http://hi.baidu.com/kakarot_java/blog/item/8c34e57360472c148601b013.html 我们知道,只有在Ext框架全部加载完后才能在客 ...

  6. N! java

    import java.util.*; import java.math.*; public class Num2{ public static void main(String args[]){ B ...

  7. 基于稀疏表(Sparse Table)的RMQ(区间最值问题)

    在RMQ的其他实现方法中,有一种叫做ST的算法比较常见. [构建] dp[i][j]表示的是从i起连续的2j个数xi,xi+1,xi+2,...xi+2j-1( 区间为[i,i+2j-1] )的最值. ...

  8. 杭电1518 Square(构成正方形) 搜索

    HDOJ1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Python pip下载安装库 临时用清华镜像命令

    pip install -i https://pypi.tuna.tsinghua.edu.cn/simple C:\Users\mu\pip 新建pip.ini [global] index-url ...

  10. 学习selenium的过程