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. Python自动化之sqlalchemy

    如果该数 据库支持 自增列 ,则 SQLAlchemy 默认 自动 设定 表中第一个 类型 为整形 的主键 为自增列 ORM介绍 orm英文全称object relational mapping,就是 ...

  2. Python自动化之pickle和面向对象初级篇

    pickle模块扩展 1 pickle之文件操作 示例1 with open("test", 'rb') as f: lines = f.readlines() print(pic ...

  3. weiphp执行的流程

    微信交互   1.用户与微信进行交互,交互的事件包括:回复公众号,扫描与公众号有关的二微码,关注(取消关注)公众号,在公众号里点击自定义菜单等 2.微信把用户的交互事件及相关参数传递给weiphp的微 ...

  4. 10个php笔试题

    Q1 第一个问题关于弱类型 $str1 = 'yabadabadoo'; $str2 = 'yaba'; if (strpos($str1,$str2)) { echo "/"&q ...

  5. [大坑]FFT学习

    [大坑]FFT学习 Macros #define fon(i,s) for(int i=0;i<s; ++i) #define fone(i,s) for(int i=0;i<=s;++i ...

  6. phpcms分页用法简介

    PHPCMS分页的用法 前面需要有引用的list,代码如下: {pc:content action="lists" catid="11" order=" ...

  7. JavaScript——this关键字

    请看下面的代码,最后alert出来的是什么呢? 1 var name = "Bob"; 2 var nameObj ={ 3 name : "Tom", 4 s ...

  8. 如何用 Robotframework 来编写优秀的测试用例

    介绍 这篇文档将会是一篇在「高层面」的怎么用 Robotframework 来编写优秀测试用例的原则.至于如何使用 Robotframework 来与您的待测试系统相作用这样的细节讨论是不包含在这篇文 ...

  9. ACM/ICPC 之 DFS范例(ZOJ2412-ZOJ1008)

    通过几道例题简单阐述一下DFS的相关题型 ZOJ2412-Farm Irrigation 直观的DFS题型,稍加变化,记录好四个方向上的通路就能够做出来 题目和接水管类似,问最少要灌溉几次,即求解最少 ...

  10. 3.UNION

    --联合结果集union(集合运算符-- group by StudentNo union ---union可以合并多个结果集 --它有两个前提和一个注意: --1.合并的结果集的列数必须完全一致 - ...