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. WindowFromPoint -- 获得包括指定点的窗体的句柄

     WindowFromPoint 函数功能: 该函数获得包括指定点的窗体的句柄. 函数原型: HWND WindowFromPoint(POINT Point): 參数: Point:指定一个被检 ...

  2. ServiceStack学习之一准备工作

    GitHub:https://github.com/ServiceStack/ServiceStack/wiki 官网介绍的前期准备知识: Wikipedia article about HTTP a ...

  3. 对“使用MyEclipse,写的jsp代码因有汉字而无法保存”问题的解决

    使用MyEclipse编辑jsp时.有时会出现"使用MyEclipse,写的jsp代码因有汉字而无法保存"的现象,怎样解决呢? Window-->Preferences--& ...

  4. 控件CListCtr详解

    1.CListCtrl控件 CListCtrl控件在数据库编程中是用得比较多的控件之一,也是Window控件中较难掌握的一个控件.他可以有四显示方式,Report.List.Icon.SmallIco ...

  5. 洛谷 P1570【NOIP2013】花匠

    题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希 望剩下的花排列得比较别致. 具 ...

  6. Java 类型信息 —— 获取泛型类型的类对象(.class)

    How to get a class instance of generics type T 考虑泛型类Foo<T>,在其成员中,如果想获取类型(type)T的类实例(class inst ...

  7. loj 6034 线段游戏

    题目大意: 给出若干条线段,用 (x1,y2),(x2,y2) 表示其两端点坐标,现在要求支持两种操作: 0 x1 y1 x2 y2 表示加入一条新的线段 (x1,y2),(x2,y2) 1 x0 询 ...

  8. 4.8 Using Ambiguous Grammars

    4.8 Using Ambiguous Grammars It is a fact that every ambiguous grammar fails to be LR and thus is no ...

  9. openStack集群封装记录

  10. Eclipse 使用Anaconda python 解释器

    问题: ubuntu16.04 Anaconda 安装成功 Eclispe 写Python代码 无法使用 (pandas库等) 原因: Eclispe 此时的python解释器==>用的并不是A ...