Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 38263   Accepted: 11891

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 - 1 or + 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.
 
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct vode
{
int time,weizhi;
};
struct vode que[];
int main()
{
int l,r,weizhi;
int n,k;
scanf("%d%d",&n,&k);
l=,r=;
weizhi=n;
que[r].weizhi=weizhi;
que[r].time=;
r++;
int flag[]={};
flag[weizhi]=;
while(que[l].weizhi!=k)
{
weizhi=que[l].weizhi;
if(weizhi->=&&weizhi-<=&&flag[weizhi-]==)
{
que[r].weizhi=que[l].weizhi-;
que[r].time=que[l].time+;
flag[weizhi-]=;
r++;
}
if(weizhi+>=&&weizhi+<=&&flag[weizhi+]==)
{
que[r].weizhi=que[l].weizhi+;
que[r].time=que[l].time+;
flag[weizhi+]=;
r++;
}
if(weizhi*>=&&weizhi*<=&&flag[weizhi*]==)
{
que[r].weizhi=que[l].weizhi*;
que[r].time=que[l].time+;
flag[weizhi*]=;
r++;
}
l++;
}
printf("%d\n",que[l].time);
return ;
}

catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)的更多相关文章

  1. 数据结构学习笔记05图 (邻接矩阵 邻接表-->BFS DFS、最短路径)

    数据结构之图 图(Graph) 包含 一组顶点:通常用V (Vertex) 表示顶点集合 一组边:通常用E (Edge) 表示边的集合 边是顶点对:(v, w) ∈E ,其中v, w ∈ V 有向边& ...

  2. poj-3278 catch that cow(搜索题)

    题目描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immedia ...

  3. Catch That Cow(广度优先搜索_bfs)

     Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48036   Accepted: 150 ...

  4. 【数据结构】图的基本操作——图的构造(邻接矩阵,邻接表),遍历(DFS,BFS)

    邻接矩阵实现如下: /* 主题:用邻接矩阵实现 DFS(递归) 与 BFS(非递归) 作者:Laugh 语言:C++ ***************************************** ...

  5. POJ - 3278 Catch That Cow 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

  6. 图 邻接表 邻接矩阵 BFS生成树 DFS生成树

  7. BFS POJ 3278 Catch That Cow

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

  8. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  9. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

随机推荐

  1. css选择器(E[att^=”val”]序号选择器)

    一.基本选择器序号 选择器 含义1. * 通用元素选择器,匹配任何元素2. E 标签选择器,匹配所有使用E标签的元素3. .info class选择器,匹配所有class属性中包含info的元素4. ...

  2. Github GUI 托管代码教程

    附录:克隆仓库到本地:git clone https://github.com/chzeze/WeiboHomeCrawl.git

  3. linux awk, xargs

    awk , 很赞的教程:http://coolshell.cn/articles/9070.html xargs, http://blog.csdn.net/andy572633/article/de ...

  4. SQL Server 中几种常见的约束关系

    1.创建唯一约束 当表中已创建主键,但又要保证其他数据列的值唯一时,可以使用唯一约束,并且唯一约束允许NULL值(只有一个) (1)展开指定的数据库: (2)右击要创建唯一约束的表,在弹出的快捷菜单中 ...

  5. struts2配置文件的加载顺序以及 struts.xml package 的配置说明

    查看StrutsPrepareAndExecuteFilter:(核心过滤器)两个功能 :预处理 和 执行 在预处理功能中 init 方法中会有加载配置文件的代码: dispatcher.init() ...

  6. springMVC中文乱码问题

    1)中文JSP页面编码统一为UTF-8后,页面可以正常显示,但从数据库中获取的数据依然显示乱码(比如下拉表单的数据): 2)当使用了springMVC提供的编码Filter拦截处理后,表单数据.从数据 ...

  7. svn上传报Authorization failed错误解决办法

    svn上传文件时没有弹出用户登录界面,而是直接报Authorization failed错误.出现该问题基本都是三个配置文件的问题,下面把这个文件列出来 svnserve.conf配置文件中的 [ge ...

  8. jquery单选框radio值改变change事件

    2015年11月25日 13:39:03 $().ready(function(){ //默认显示#input_div if ($("input[name='type']:checked&q ...

  9. Selenium WebDriver 处理cookie

    在使用webdriver测试中,很多地方都使用登陆,cookie能够实现不必再次输入用户名密码进行登陆. 首先了解一下Java Cookie类的一些方法. 在jsp中处理cookie数据的常用方法: ...

  10. Pythonj~module

    常数,变量 特殊变量:__xxx__  可以被直接引用 private函数:_xxx __xxx__ 外部不需要引用的函数全部定义成private,只有外部需要引用的函数才定义为public. 导入 ...