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

别看题简单,满满的坑啊!!你初始在n,先让你移动到k,有三种移动,—1,+1,*2。问你几步能到k。
最开始无脑搜,直接T了。后来发现bfs虽然是有点暴力的感觉但是还是要有思维在里面的!如果n>k,n只能一点点减到k。还有数组开大点,以防*2后越界。
代码如下:
 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int n,k,ans;
bool vis[];
struct node
{
int pos,step;
};
bool check (int x)
{
if (vis[x])
return ;
if (x<||x>)
return ;
else
return ;
}
void bfs (node now)
{ queue<node>q;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
if (check(temp.pos-))
{
node x;
x.pos=temp.pos-;
x.step=temp.step+;
vis[x.pos]=;
if (x.pos==k)
{
ans=x.step;
return ;
}
q.push(x);
}
if (check(temp.pos+)&&temp.pos<k)
{
node x;
x.pos=temp.pos+;
x.step=temp.step+;
vis[x.pos]=;
if (x.pos==k)
{
ans=x.step;
return ;
}
q.push(x);
}
if (check(temp.pos*)&&temp.pos<k)
{
node x;
x.pos=temp.pos*;
x.step=temp.step+;
vis[x.pos]=;
if (x.pos==k)
{
ans=x.step;
return ;
}
q.push(x);
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
while (~scanf("%d%d",&n,&k))
{
memset(vis,false,sizeof vis);
node now;
now.pos=n;
now.step=;
ans=;
if (n>=k)
printf("%d\n",n-k);
else
{
bfs(now);
printf("%d\n",ans);
}
}
return ;
}

POJ 3278 Catch That Cow (有思路有细节的bfs)的更多相关文章

  1. BFS POJ 3278 Catch That Cow

    题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  3. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

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

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

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

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  6. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  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(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

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

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

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

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

随机推荐

  1. STM32时钟设置

    一.使用外部时钟,并设置为72MHz void SetSysClockToHSE(void) { ErrorStatus HSEStartUpStatus; /* SYSCLK, HCLK, PCLK ...

  2. CPU C-States Power Saving Modes

    http://www.hardwaresecrets.com/article/611 Everything You Need to Know About the CPU C-States Power ...

  3. jmeter添加自定义扩展函数之MD5加密

    1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...

  4. Python 进阶_OOP 面向对象编程_类属性和方法

    目录 目录 类属性 调用类属性 查看类属性 特殊的类属性 类方法 真构造器 __new__ 类属性 在理解类属性之前要先搞清楚 实例属性 和 函数属性 之间的区别: 1. 实例属性:指的是实例化类对象 ...

  5. java方法调用及传参

    静态方法:有static修饰的方法. 非静态方法:没有static修饰的方法. 方法调用: 一静态方法调用 静态方法/属性 1)一个类:直接调用. 2)不同类/不同文件: a: 类名.属性名/方法名 ...

  6. C++怎样通过嵌入汇编写一个函数

    参考:http://msdn.microsoft.com/en-us/library/h5w10wxs.aspx 普通的函数,Compiler会自动生成prologue和epilogue,但是通过在函 ...

  7. VB - sendKey

    Set WshShell=WScript.CreateObject("WScript.Shell") WshShell = SendKeys string “string”:表示要 ...

  8. js记住密码

    $(function () { if (getCookie("rmbUser") == "true") {   $("#xuanzong") ...

  9. 《穷爸爸富爸爸——Cashflow》

    读<穷爸爸富爸爸>大约两年前了,当时对理财没什么概念,除了支付宝,就是京东小金库,哪个利率高就存哪个里.记忆中除了感觉这应该是有一定经济基础的人通常做的事,工薪阶级的自己还未达标,工资除了 ...

  10. 如何通过HTTP API 调取tushare的股票数据

    长久以来,Tushare一直以固定的Python SDK方式为大家提供数据服务. 虽然在基于Python的数据分析和Python的量化策略开发很方便,但习惯用其他语言的同学们表示了“抗议”,于是在Tu ...