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. composer ip2city配置

    //根据ip获取地址信息composer require "mylukin/ip2city: dev-master" // vendor/mylukin/ip2city/src/I ...

  2. ELK日志系统:Filebeat使用及Kibana如何设置登录认证(转)

    原文地址:http://www.cnblogs.com/yjmyzz/p/filebeat-turorial-and-kibana-login-setting-with-nginx.html 根据el ...

  3. python 标准输入输出sys.stdout. sys.stdin

    import sys, time ## print('please enter your name:')# user_input=sys.stdin.readline()# print(user_in ...

  4. 计数器counter

    今天就讲了2个属性:1.计数器 2.列规则 列规则很简单:column-count:3; (列的具体个数) column-width:30px;(列宽)N个浏览器不兼容column-gap:10px; ...

  5. Android Studio 解析json文件出现中文乱码解决方法

    作为一个Android开发初学者,好不容易找到解决方法,跟大家分享一下, 其实很简单,只要保持服务器上的文件(date2.json)与软件的编码方式一样就行. 我用的Android Studio是ut ...

  6. 时间处理:计算下一天日期,如输入"2004/12/31"(注释2014年12月31日),则输出"2005/1/1".

    /* ============================================================================ Name : Exercise.c Au ...

  7. andorid 全部对话框

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  8. iOS.Operation-on-ZipFile

    Operation on ZipFile Reference 在Mac OS X和iOS中操作.zip文件(例如创建zip文件, 从zip文件中抽取数据): 1. http://stackoverfl ...

  9. CF487E Tourists - Tarjan缩点 + 树剖 + multiset

    Solution 先Tarjan求出点双联通分量 并缩点. 用$multiset$维护 点双内的最小点权. 容易发现, 点双内的最小点权必须包括与它相连的割边的点权. 所以我们必须想办法来维护. 所以 ...

  10. PC 上的 LVM 灾难修复

    LVM 介绍 LVM 简介 LVM 是逻辑盘卷管理(Logical Volume Manager)的简称,最早是 IBM 为 AIX 研发的存储管理机制.LVM 通过在硬盘和分区之间建立一个逻辑层,可 ...