Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 89836   Accepted: 28175

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.
 
 
起点在n,终点是k,每一步可以是+1、-1或*2,最少走几步?
 
 #include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int a[];//一个2倍大小的数组代表可以走到的位置,因为有乘二所以要开二倍以防RE,a[i]=x --> 走到坐标i需要x步
int main()
{
int n, k, t;
queue<int>q;
scanf("%d%d", &n, &k);
memset(a, -, sizeof(a));//所有坐标初始化为-1
a[n] = ; //n走到n当然是需要步
q.push(n); //当前位点入队
while (!q.empty())
{
t = q.front(); //读取队首
q.pop(); //删除队首
if (t == k) //若到达终点则直接输出并结束
{
printf("%d\n", a[k]);
return ;
}
if (t - >= && a[t - ] == -)//可能到达的结点入队,要判断是否越界,走过的不再走
{
a[t - ] = a[t] + ; q.push(t - );//下一步走到的位点所需步数是当前位点的步数+1
}
if (t + < && a[t + ] == -)
{
q.push(t + ); a[t + ] = a[t] + ;
}
if (t * < && a[t * ] == -)
{
q.push(t * ); a[t * ] = a[t] + ;
}
}
}

简单的bfs

 

超超超简单的bfs——POJ-3278的更多相关文章

  1. BFS POJ 3278 Catch That Cow

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

  2. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  3. 搜索 || BFS || POJ 3278 Catch That Cow

    农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...

  4. 转帖: 一份超全超详细的 ADB 用法大全

    增加一句 连接 网易mumu模拟器的方法 adb  connect 127.0.0.1:7555 一份超全超详细的 ADB 用法大全 2016年08月28日 10:49:41 阅读数:35890 原文 ...

  5. 【BFS】POJ 3278

    POJ 3278 Catch That Cow 题目:你要去抓一头牛,给出你所在的坐标和牛所在的坐标,移动方式有两种:要么前一步或者后一步,要么移动到现在所在坐标的两倍,两种方式都要花费一分钟,问你最 ...

  6. 超全超详细的HTTP状态码大全(推荐抓包工具HTTP Analyzer V6.5.3)

    超全超详细的HTTP状态码大全 本部分余下的内容会详细地介绍 HTTP 1.1中的状态码.这些状态码被分为五大类: 100-199 用于指定客户端应相应的某些动作. 200-299 用于表示请求成功. ...

  7. 搜索入门_简单搜索bfs dfs大杂烩

    dfs题大杂烩 棋盘问题  POJ - 1321 和经典的八皇后问题一样.  给你一个棋盘,只有#区域可以放棋子,同时同一行和同一列只能有一个棋子. 问你放k个棋子有多少种方案. 很明显,这是搜索题. ...

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

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

  9. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

随机推荐

  1. Mybatis在oracle批量更新

    最近公司业务中为了提高效率要做mybatis批量更新,但是到了oracle数据库中做了好几次都没成功,后来发现mybatis最后少了个分号,可能是Mybatis内部做了异常try  catche  处 ...

  2. 多个form表单的提交

    if(zhengchang_stop&&no_zhengchang_wancheng&&respon_info_lists){ $('form[name="f ...

  3. LVS工作模式与调度算法

    LVS三种工作模式.十种调度算法介绍 工作模式介绍: 1.Virtual server via NAT(VS-NAT) 优点:集群中的物理服务器可以使用任何支持TCP/IP操作系统,物理服务器可以分配 ...

  4. 【LeetCode】141. Linked List Cycle

    题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...

  5. LinkedList原理及源码解析

    简介 LinkedList是一个双向线性链表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度, ...

  6. XML读取两种方法

    //第一种SAX方法解析 package a20170722.xmlex; import java.io.File; import java.util.ArrayList; import java.u ...

  7. Javascript之学习笔记每日更新

    1.输出文本 document.write(Date());输出当前时间 2.使用Jacascript改变HTML元素 //定义一个p标签,此p标签带有id元素 <p id="demo ...

  8. Tween 若干年后我尽然还要学数学 曲线到底是什么鬼啊

    var Tween = { linear: function (t, b, c, d){ //匀速 return c*t/d + b; }, easeIn: function(t, b, c, d){ ...

  9. 今天真开心,终于知道怎么打包apk了

    1.函数上下文的产生,并不是函数定义时决定的,而是函数调用时产生的:来个栗子 function creep(){return this;} console.log(creep()) var sneak ...

  10. localStorage与location的用法

    1.localStorage 是h5提供的客户端存储数据的新方法: 之前,这些都是由 cookie 完成的.但是 cookie 不适合大量数据的存储,因为它们由每个对服务器的请求来传递,这使得 coo ...