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. MyBatis框架之异常处理

    MyBatis框架定义了许多的异常类,之所以定义这么多的异常类,应该是将每一种异常情况都独立出来,这样在出现异常时,定位就很明确了.而我们平时写代码时,都是瞎搞一通,异常类大多也是随便定义,或者是使用 ...

  2. ES6中模块加载出现的问题

    1.如何在浏览器中import模块 在使用模块加载时不同浏览器有不同的行为 使用 import 加载模块时,需要把script标签的type属性改为module.此时Firefox浏览器支持impor ...

  3. socket | netcat 模拟

    #!/opt/local/bin/python2.7 #coding=utf-8 ''' 取代netcat 两台主机中其中一台控制另一台 得到北控方的shell ''' import sys impo ...

  4. Jenkins执行脚本文件

    Jenkins执行脚本文件如下(startup.sh): #!/bin/bash #这里可替换为你自己的执行程序,其他代码无需更改 export JAVA_HOME=/usr/src/java/jdk ...

  5. MySQL复制表结构和内容到另一个表中

    一:(低版本的mysql不支持,mysql4.0.25 不支持,mysql5已经支持了)1.复制表结构到新表CREATE TABLE 新表LIKE 旧表 2.复制旧表的数据到新表(假设两个表结构一样) ...

  6. Hive date_add 和 date_diff 函数

    date_add 函数 例子:date_add('day',-1,current_date) date_diff 函数 例子:date_diff('day',cast(a.dt1 as timesta ...

  7. JS-for..of

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of 刚刚上网上看到<V8 ...

  8. h5调用微信支付功能

    支付按钮的点击事件 $(´.Save_Patient_Msg´).click(function(){ $(´.Save_Patient_Msg´).off(´click´); var hrdfId = ...

  9. Javascript对checkbox勾选判断,错误提示和按钮变色操作

    同意相关条款未打钩时,登录按钮为灰色且无法提交,点击灰色的登录按钮提示同意相关条款,打钩后变成亮色且可以提交信息. 勾选框及文字: <div class="check-rule&quo ...

  10. Html5 学习笔记 【PC固定布局】 实战7 风景欣赏 联系我们

    风景欣赏最终效果: 关于公司最终效果: 风景欣赏Html代码: <!DOCTYPE html> <html lang="zh-cn"> <head&g ...