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.
 
感受:1.BFS求最短路这点没想到啊,周赛的时候拼命的去想DFS,然后就是TLE,RE,根本没有想到BFS求最短路。
        2.要加标记数组,不然会死循环。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 220000
int n,k;
int vis[maxn]; struct Node
{
int x, time; }node[maxn]; void bfs()
{
queue<Node> Q;
Node r,t,f;
r.time = ; r.x = n;
vis[n] = ;
Q.push(r);
while(!Q.empty())
{
f = Q.front();
Q.pop();
if(f.x == k)
{
printf("%d\n", f.time);
return;
}
if(f.x < k&&!vis[f.x*])
{
t.x = f.x*;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
if(f.x+<=k && !vis[f.x+])
{
t.x = f.x+;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
if(f.x->= && !vis[f.x-])
{
t.x = f.x-;
vis[t.x] = ;
t.time = f.time+;
Q.push(t);
}
}
}
int main()
{
while(~scanf("%d%d", &n, &k))
{
if(k <= n)
{
printf("%d\n", n-k);
continue;
}
memset(vis, , sizeof vis);
bfs();
}
return ;
}
       

POJ3278 Catch That Cow(BFS)的更多相关文章

  1. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  2. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  3. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  4. poj3278 Catch That Cow(简单的一维bfs)

    http://poj.org/problem?id=3278                                                                       ...

  5. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  6. poj3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 73973   Accepted: 23308 ...

  7. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

随机推荐

  1. 漫谈MySQL primaryKey

    主键没有着明确的概念定义,其是索引的一种,并且是唯一性索引的一种,且必须定义为“PRIMARY KEY”,是只可意会不可言传的东西.下面让我用通俗,甚至有些低俗的语言为您简单介绍一下MySQL的主键. ...

  2. Java 四种线程池的用法分析

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? new Thread(new Runnable() { @Override public void run() { ...

  3. Android单元测试: 首先,从是什么开始

    Android单元测试: 首先,从是什么开始 http://chriszou.com/2016/04/13/android-unit-testing-start-from-what.html 这是一系 ...

  4. 基于drools创建自己的关系操作符

    我们知道drools提供了12种关系操作符 但是有些时候这12种操作符依然不能满足我们的业务需求,我们可以扩展自己的操作符,下面是为某一航空公司做项目时扩展了操作符,在这分享下 首先,我们要实现的逻辑 ...

  5. ARM体系结构_DAY2

    程序状态寄存器(CPSR) Mode位[4:0]:处理器模式为 USER模式不能直接切换到特权模式,在特权模式下可以直接修改mode位[4:0]为10000,切换到USER模式. T bit位[5]: ...

  6. Oracle11g环境设置-windows环境

    新建环境变量(系统变量),变量名:ORACLE_HOME 变量值:E:\app\Administrator\product\11.2.0\dbhome_1 新建环境变量(系统变量),变量名:ORACL ...

  7. jquery之提示信息

    //生成优惠券并分发 function saveCouponAssign(){ //发行券种 var couponTypeId = $("#couponTypeId").combo ...

  8. 命令行下如何安装VMware Tools并与windows资料共享

    安装VMware Tools: 选择菜单栏“虚拟机”——“安装VMware tools” ,等待系统自动更换ISO光盘 mount /dev/cdrom /mntcd /mnttar zxvf VMw ...

  9. mysql 5.6 设置慢查询

    mysql 5.6 开启慢查询日志 slow_query_log = on #开启慢查询 1 或者 on long_query_time = 3 #记录超过的时间,单位是秒,默认是10s slow_q ...

  10. POJ 1011 - Sticks DFS+剪枝

    POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    ...