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 X - 1 or X + 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有三种移动方法:1、向前走一步,耗时一分钟。

2、向后走一步,耗时一分钟。

3、向前移动到当前位置的两倍N*2,耗时一分钟。

问FJ抓到奶牛的最少时间(奶牛不动)

解题思路:很明显我们求最短的时间就相当于求走的最少步数,所以我们就很容易想到用广搜BFS了,把每次FJ可以到的位置都压入队列,然后用一个vis数组标记FJ是否已经去过该位置了,再用 一个step数组存储FJ到该位置所需要的最短时间即可。要注意的是,他的位置也有范围是0-100000,开始我就是因为右区间少了个=号,WA了好几次,所以还是要细心。难的做不出只好做简单的了。。。

附上代码:

 #include<iostream>
#include<cstdio>
#include<string.h>
#include<queue>
using namespace std;
int vis[]; //标记是否走过
int step[]; //储存到该处的最短时间
int n,k;
queue<int> que;
int ans; int BFS()
{
que.push(n);
step[n]=;
vis[n]=;
while(que.size())
{
int p=que.front();
que.pop();
if(p==k) break;
for(int i=;i<;i++)
{
int dx;
if(i==) dx=p-;
if(i==) dx=p+;
if(i==) dx=*p;
if(dx>=&&dx<=&&vis[dx]==)
{
que.push(dx);
step[dx]=step[p]+;
vis[dx]=;
} }
}
return step[k];
} int main()
{
while(cin>>n>>k)
{
memset(vis,,sizeof(vis));
memset(step,,sizeof(step));
ans=BFS();
cout<<ans<<endl;
}
return ;
}

POJ3287(BFS水题)的更多相关文章

  1. POJ 3984 - 迷宫问题 - [BFS水题]

    题目链接:http://poj.org/problem?id=3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, ...

  2. hdu1240 bfs 水题

    原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...

  3. POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

    http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a ...

  4. nyoj--523--亡命逃窜(BFS水题)

    亡命逃窜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧.不过英雄不是这么好当的.这个可怜的娃被魔 ...

  5. POJ 3126 Prime Path bfs, 水题 难度:0

    题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为 ...

  6. codeforces 811 D. Vladik and Favorite Game(bfs水题)

    题目链接:http://codeforces.com/contest/811/problem/D 题意:现在给你一个n*m大小的图,你输出一个方向之后,系统反馈给你一个坐标,表示走完这步之后到的位子, ...

  7. CYJian的水题大赛

    实在没忍住就去打比赛了然后一耗就是一天 最后Rank19还是挺好的(要不是乐多赛不然炸飞),这是唯一一套在Luogu上号称水题大赛的而实际上真的是水题大赛的比赛 好了我们开始看题 T1 八百标兵奔北坡 ...

  8. hihocoder 1322 - 树结构判定 - [hiho一下161周][模板题/水题]

    题目链接:http://hihocoder.com/problemset/problem/1322 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个包含 N 个顶 ...

  9. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

随机推荐

  1. Java中clone的写法

    Cloneable这个接口设计得十分奇葩,不符合正常人的使用习惯,然而用这个接口的人很多也很有必要,所以还是有必要了解一下这套扭曲的机制.以下内容来自于对Effective Java ed 2. it ...

  2. Flutter - TabBar导航栏切换后,状态丢失

    上一篇讲到了 Flutter - BottomNavigationBar底部导航栏切换后,状态丢失 里面提到了TabBar,这儿专门再写一下吧,具体怎么操作,来不让TabBar的状态丢失.毕竟大家99 ...

  3. 常见 Bash 内置变量介绍

    目录 $0$1, $2 等等$#$* 与 "$*"$@ 与 "$@"$!$_$$$PPID$?$BASH$BASH_VERSION$EUID 与 $UID$GR ...

  4. Python迭代器(Iterator)

    概述 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退. 延迟计算或惰性求值 (Lazy evaluation) 迭代器不要求你 ...

  5. css-preprocessors

    what ? 预处理器是css 能够使用 变量.操作符.函数.mixins.interpolations 等类似于js 功能的一种语言. 目前比较常用是三种:SASS.less .stylus . W ...

  6. bootstrap完善按钮组bug

    .btn.active { border: 1px solid #ff9400 !important; color: #ff9400 !important; } <div class=" ...

  7. wordcount程序

    wordcount程序算是相比于前几次作业来说比较难得一个作业了.进行了一次真的自己编写程序.WC程序实现了对txt文件中的数据的计数,算出程序中有多少单词.字符数以及行数.这次的程序编程是采用的C语 ...

  8. Linux内核分析作业八

    进程的切换和系统的一般执行过程 贾瑗 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.com/course/USTC-1000029 ...

  9. linux内核分析第六周学习笔记

    LINUX内核分析第六周学习总结 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程 http://mooc.study.163.c ...

  10. HTML页面打印

    <style media=print>.Noprint{display:none;}</style> <object id="WebBrowser" ...