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. ViewPager学习之仿微信主界面

    由于素材的原因,这里都是从网上找的图片,所以所谓的仿微信实际上最后成了下图这货.. .,点击变色也是自己用的windows自带绘图的颜料桶填充的空白. .. watermark/2/text/aHR0 ...

  2. 划分问题(dp)

    给出一个整数集合  , 将这个集合分成2个 和相等的集合. 思路就是  总和的一半作为一个目标容量,在集合中寻找能否够有元素的和恰好为这个目标容量,也就转化为一个类似01背包问题. 这篇文章讲的比較具 ...

  3. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  4. Ubuntu安装JDK及环境变量配置(sun java)

    捣鼓了尽一天的时间,终于把sun的java安装上了,不是openjava了,网上试了好多的方法好多都是不可以的,所以当自己成功后就立马把方法贴出来,以方便后来者少走弯路,此文的方法绝对可行! 这里先简 ...

  5. (四)Java 基础语法

    Java 基础语法 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如,一 ...

  6. HIbernate- SQLQuery 简易

    package cn.demo; import java.util.Arrays; import java.util.List; import org.hibernate.SQLQuery; impo ...

  7. 理解dropout——本质是通过阻止特征检测器的共同作用来防止过拟合 Dropout是指在模型训练时随机让网络某些隐含层节点的权重不工作,不工作的那些节点可以暂时认为不是网络结构的一部分,但是它的权重得保留下来(只是暂时不更新而已),因为下次样本输入时它可能又得工作了

    理解dropout from:http://blog.csdn.net/stdcoutzyx/article/details/49022443 http://www.cnblogs.com/torna ...

  8. [译]NUnit--Installation(三)

    Installation NUnit安装程序默认安装文件路径为C:\Program Files\NUnit 2.6.2.根据用户选择安装的选项,安装文件有三个子文件夹:bin.doc.samples. ...

  9. 转 Dos和linux格式转换(转)

    错误提示: bad interpreter: No such file or directory: /bin/sh 错误分析: 因为操作系统是windows,在windows下编辑的脚本,所以有可能有 ...

  10. luogu 4630 [APIO2018] Duathlon 铁人两项

    题目大意: 无向图上找三个点 a b c使存在一条从a到b经过c的路径 求取这三个点的方案数 思路: 建立圆方树 这个圆方树保证没有两个圆点相连或两个方点相连 对于每个节点x 设该节点为路径的中间节点 ...