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. SEO 搜索引擎优化培训01

    百度搜索风云榜:http://top.baidu.com/boards 页面上的因素:对搜索引擎而言

  2. openwrt-安装-驱动-应用-lcd2004a实验

    1. 板子f403tech的RT5350的板子和 (1)openWRT系统的定义和特点         OpenWrt是一个高度模块化.高度自己主动化的嵌入式Linux系统.拥有强大的网络组件.经常被 ...

  3. easyUI datagrid 时间格式化

    从后台传过来的数据,其中含有日期字段,那么在前端的easyUI这里显示的话,会显得比较怪异,一大串,中间是个T,后面一大堆零,不知道是什么意思. 看来要进行格式化. 问题是,在哪里格式化? 如果在后端 ...

  4. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] D "Or" Game 枚举+前缀后缀

                                                                            D. "Or" Game       ...

  5. 利用函数计算构建微信小程序的Server端

    10分钟上线 - 利用函数计算构建微信小程序的Server端-博客-云栖社区-阿里云 https://yq.aliyun.com/articles/435430 函数计算  读写 oss import ...

  6. Linux/Android——input系统之 kernel层 与 frameworks层交互 (五)【转】

    本文转载自:http://blog.csdn.net/jscese/article/details/42291149 之前的四篇博文记录的都是linux中的input体系相关的东西,最底层以我调试的u ...

  7. 基于ASP.NET MVC的快速开发平台,给你的开发一个加速度!

    基于ASP.NET MVC的快速开发平台,给你的开发一个加速度! bingo炸了 2017/4/6 11:07:21 阅读(37) 评论(0) 现在的人做事情都讲究效率,最好能达到事半功倍那种效果,软 ...

  8. CSYZDay1模拟题解

    T1.game [问题描述] LZK发明一个矩阵游戏,大家一起来玩玩吧,有一个N行M列的矩阵.第一行的数字是1,2,…M,第二行的数字是M+1,M+2…2*M,以此类推,第N行的数字是(N-1)*M+ ...

  9. CentOS查看CPU、内存、网络流量和磁盘 I/O【详细】

    安装 yum install -y sysstat sar -d 1 1 rrqm/s: 每秒进行 merge 的读操作数目.即 delta(rmerge)/swrqm/s: 每秒进行 merge 的 ...

  10. bzoj1051受欢迎的牛(Tarjan)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4776  Solved: 2542 Description ...