Catch That Cow

Problem 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

题意描述:

把一个数变成另一个数,可以通过加1或减1或者变成现在数的2倍,求最少的步数

程序代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
struct data{int x;int y;int step;
};
int book[200010];
int main()
{int a,b,k;struct data A,B,next;queue<data>que;while(scanf("%d%d",&a,&b)!=EOF){while (!que.empty()){que.pop();}A.x=a;A.step=0;memset(book,0,sizeof(book));book[A.x]=1;que.push(A);while(!que.empty()){B=que.front();if(B.x==b){printf("%d\n",B.step);break;}que.pop();for(k=0;k<3;k++){next=B;if(k==0)next.x=next.x+1;if(k==1)next.x=next.x-1;if(k==2)next.x=next.x*2;next.step++;if(next.x>=0&&next.x<=200000&&book[next.x]==0){book[next.x]=1;que.push(next); }}}}return 0;
}

HDU-2717-Catch That Cow(bfs)相关推荐

  1. HDU 2717 Catch That Cow(BFS)

    题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...

  2. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  3. POJ 3278 / hdu 2717 Catch That Cow (广搜)

    POJ 3278 HDU 2717 广搜题,用一个数组标记就可以过,不标记的话会超内存. 另外,poj的数据要比hdu强一些,比如0 100,这种数据.不特判的话会RE.不过如果不特判,在poj上用C ...

  4. C - Catch That Cow(BFS)

    找奶牛 有三种移动方式,作为BFS搜索的方向 利用 当 n > k 时只能向后走,优化结果 #include<iostream> #include<cstring> #i ...

  5. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...

  6. HDU2717 Catch That Cow(bfs)

    题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...

  7. 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)

    [HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...

  8. POJ3278Catch That Cow(BFS)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 70652   Accepted: 22216 ...

  9. (BFS)Catch That Cow(poj3278)

    题目: 农夫知道一头牛的位置,想要抓住它.农夫和牛都于数轴上 ,农夫起始位于点 N(0<=N<=100000) ,牛位于点 K(0<=K<=100000) .农夫有两种移动方式 ...

最新文章

  1. Http 状态码一览表
  2. mysql 备份_MySQL数据库备份实操
  3. python数据结构 树_Python数据结构——AVL树的实现
  4. 美团(Leaf)分布式ID算法
  5. 项目管理工程师:第二章信息系统服务管理
  6. SSM框架笔记13:Spring MVC基础
  7. 为informix数据库中的表创建同义词
  8. Linux 给普通用户分配root权限或给用户分配多个用户组
  9. css鼠标滑过变大,css 鼠标移上去会变大(示例代码)
  10. 三年程序员成功转型项目经理
  11. Python绘制心形图案
  12. Centos7快速搭建服务器加速
  13. pbe近似_量子化学中的主要近似.doc
  14. 计算机参数含义,电脑内存条参数各有什么含义?
  15. ffmpeg入门篇-ffmpeg是怎么转码的?
  16. Android App签名(证书)校验过程源码分析
  17. 华为OJ——参数解析
  18. windows10 英文路径下文件显示中文名称
  19. android蓝牙室内定位,基于蓝牙的室内定位系统设计与实现
  20. java2d游戏代码_JAVA游戏编程学习笔记(三)Java 2D游戏底层绘图框架

热门文章

  1. nginx.conf文件详解
  2. 20155322 《Java程序设计》课堂实践项目 数据库-3-4
  3. Thrift框架使用C++的一个demo
  4. .NET中方法的注意事项 明细
  5. 蓝点中文_linux2.0 实验二 简单shell命令
  6. Error configuring application listener of class org.springframework.web.context.ContextLoaderListene
  7. 性能测试之JMeter主页面布局
  8. Jmeter如何进行http接口测试
  9. 都是执行软件测试,差异点在那里
  10. 性能测试第一步——查看设备的硬件配置