Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 54911   Accepted: 17176

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.
题解:
宽度优先搜索bfs
代码:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
#define N 100005
struct Node
{
int x;
int step;
};
queue<struct Node>q;
bool visit[N]; bool ok(int x)
{
if(x>= && x<=)
return ;
return ;
} int cal(int i,int x)
{
if(i==)
return x+;
else if(i==)
return x-;
else
return x*;
}
void bfs(int a,int b)
{
memset(visit,,sizeof(visit));
while(!q.empty())
{
q.pop();
}
Node aa;
aa.step=;
aa.x=a;
q.push(aa);
visit[aa.x]=;
while(!q.empty())
{
Node tmp=q.front();
q.pop();
for(int i=; i<; i++)
{
int y=cal(i,tmp.x);
if(ok(y)&&!visit[y])
{
if(y==b)
cout<<tmp.step<<endl;
else
{
aa.step=tmp.step+;
aa.x=y;
q.push(aa); }
          visit[y]=;
} }
}
}
int main()
{
int n,k;
cin>>n>>k;
if(k>n)
bfs(n,k);
else
cout<<(n-k);
return ;
}

POJ_3278_Catch That Cow的更多相关文章

  1. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  2. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  3. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  4. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  5. 细读cow.osg

    细读cow.osg 转自:http://www.cnblogs.com/mumuliang/archive/2010/06/03/1873543.html 对,就是那只著名的奶牛. //Group节点 ...

  6. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  7. raw,cow,qcow,qcow2镜像的比较

    在linux下,虚拟机的选择方式有很多,比如vmware for linux,virtual box,还有qemu,在以前,使用qemu的人不多,主要是使用起来有些麻烦,但现在随着Openstack的 ...

  8. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

  9. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

随机推荐

  1. HTML5:理解head

    HTML文档的head部分,通常包含指定页面标题.为搜索引擎提供关于页面本身的信息.加载样式表,以及加载JavaScript文件(出于性能考虑.多数时候放在页面底部</body>标签结束前 ...

  2. C++ Primer高速入门之五:有用的模板库

    更新:又一次排版代码格式 除上篇博客介绍的基本数据类型外,C++ 还定义了一个内容丰富的抽象数据类 型标准库. 包含 string 和 vector,它们分别定义了字符串和矢量(集合).string ...

  3. Python3标准库(二) re模块

    正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...

  4. update-java-alternatives 更改默认Java环境

    Ubuntu/debian 更改默认Java环境 我的电脑里安装了两个版本的Java,一个是java-6-sun,还有一个是java-gcjgcj是在JVM非常缓慢的时候诞生的,他可以把Java代码编 ...

  5. 关于Time Series Database

    今天观察了下clojure之父datomic.com的数据库 有点类似sequence数据库的变种 不过这类数据库,感觉用在移动端会更有市场: 1. 移动端需要这种可以本地存储,无删除,联网只增加,只 ...

  6. 【Codevs 3115】高精度练习之减法

    http://codevs.cn/problem/3115/ 板子题~ // <H.cpp> - Sun Oct 9 12:58:23 2016 // This file is made ...

  7. Lexer and parser generators (ocamllex, ocamlyacc)

    Chapter 12 Lexer and parser generators (ocamllex, ocamlyacc) This chapter describes two program gene ...

  8. 洛谷 P2577 [ ZJOI 2005 ] 午餐 —— DP + 贪心

    题目:https://www.luogu.org/problemnew/show/P2577 首先,想一想可以发现贪心策略是把吃饭时间长的人放在前面: 设 f[i][j] 表示考虑到第 i 个人,目前 ...

  9. 通过CSS控制页面中的内容垂直居中的方法

    方法一:通过行高(line-height)定位 line-height通常是用于调节一段文字的行与行之间的距离,或者说两行文字之间的距离,如果行高是500px,那么每一行中的文字距离本行的顶部就是25 ...

  10. VIDIOC_S_INPUT 作用 (转载)

    转载:http://blog.csdn.net/kickxxx/article/details/7088658 G_INPUT和S_INPUT用来查询和选则当前的input 一个video设备节点可能 ...