http://poj.org/problem?id=3278
Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 146388   Accepted: 44997

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.
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
int vis[];
int ans[];
int n , m ; void bfs(int b)
{
queue<int>q;
memset(vis , , sizeof(vis));
memset(ans , , sizeof(ans));
if(n < m)
{ q.push(b);
vis[b] = ;
ans[b] = ;
while(!q.empty())
{ int x = q.front();
if(x == m)
{
printf("%d\n" , ans[m]);
break ;
}
q.pop() ;
if(x >= && x <= && !vis[x*])
{
vis[*x] = ;
ans[*x] += vis[x] + ans[x];
q.push(*x);
}
if(x >= && !vis[x+])
{
vis[x+] = ;
ans[x+] += vis[x] + ans[x];
q.push(x+);
}
if(x - >= && !vis[x-])
{
vis[x-] = ;
ans[x-] += vis[x] + ans[x];
q.push(x-);
}
} }
else
{
printf("%d\n" , n - m);
} } int main()
{ while(~scanf("%d%d" , &n , &m))
{
bfs(n);
} return ;
}

bfs(最短路径)的更多相关文章

  1. 【POJ3182】The Grove BFS 最短路径周围

    意甲冠军:给定一个N*M图.,间'X'代表树木(树木必须汇集到森林,非分离),然后,'.'它代表的空间.'*'它代表的起点.现在它需要从起点.一圈,最后回到起点,所经过最少点数. 题目中给的'+'就是 ...

  2. [POJ 3984] 迷宫问题(BFS最短路径的记录和打印问题)

    题目链接:http://poj.org/problem?id=3984 宽度优先搜索最短路径的记录和打印问题 #include<iostream> #include<queue> ...

  3. 推箱子小游戏《格鲁的实验室》13关 - bfs最短路径

    下载了一款推箱子小游戏,第13关的时候怎么也破不了最佳纪录(最少步数是9而我们最好的方案是10步),因为数据比较小(6*8的方阵),所以写了个BFS来找最短路. 游戏的目标是把小黄人推到黄色球,小绿人 ...

  4. DFS和BFS

    BFS 代码步骤: 1.写出每个点和每个点的邻接点的对应关系 2.方法参数:传一个对应关系和起始点 3.创建一个队列,然后每次都移除第一个,然后把移除的邻接点添加进去,打印取出的第一个,然后循环,一直 ...

  5. 算法导论—无向图的遍历(BFS+DFS,MATLAB)

    华电北风吹 天津大学认知计算与应用重点实验室 最后改动日期:2015/8/22 无向图的存储方式有邻接矩阵,邻接链表,稀疏矩阵等. 无向图主要包括双方面内容,图的遍历和寻找联通分量. 一.无向图的遍历 ...

  6. POJ 3083 Children of the Candy Corn (DFS + BFS)

    POJ-3083 题意: 给一个h*w的地图. '#'表示墙: '.'表示空地: 'S'表示起点: 'E'表示终点: 1)在地图中仅有一个'S'和一个'E',他们为位于地图的边墙,不在墙角: 2)地图 ...

  7. 数据结构之二叉搜索树、AVL自平衡树

    前言 最近在帮公司校招~~ 所以来整理一些数据结构方面的知识,这些知识呢,光看一遍理解还是很浅的,看过跟动手做过一遍的同学还是很容易分辨的哟~ 一直觉得数据结构跟算法,就好比金庸小说里的<九阳神 ...

  8. 算法与数据结构基础 - 图(Graph)

    图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...

  9. 关于图算法 & 图分析的基础知识概览

    网址:https://learning.oreilly.com/library/view/graph-algorithms-/9781492060116/ 你肯定没有读过这本书,因为这本书的发布日期是 ...

随机推荐

  1. git常用相关操作

    // 账号密码克隆远程项目 git clone http://账号:密码@项目地址 // 查看当前状态 git status // 查看修改内容 git diff // 添加并提交 git add . ...

  2. 尖沙咀到底谁说的算?!--- CSS层叠

    前些天,我朋友发了这个段CSS我. //css *{ color:#fff ; } div{ color:#000 !important; } //html <div><span&g ...

  3. 【转】C/C++产生随机数

    转自:https://www.cnblogs.com/vectors07/p/8185215.html C/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,C语言/C++里 ...

  4. gcc的-D,-w,-W,-Wall,-O3这些参数的意义

    一.-D 其意义是添加宏定义,这个很有用. 当你想要通过宏控制你的程序,不必傻乎乎的在程序里定义,然后需要哪个版本,去修改宏. 只需要在执行gcc的时候,指定-D,后面跟宏的名称即可. 示例: gcc ...

  5. [BZOJ] IOI2015 Boxes纪念品盒

    问题描述 IOI2015 开幕式正在进行最后一个环节.按计划在开幕式期间,每个代表队都将收到由主办方发放的一个装有纪念品的盒子.然而所有志愿者都被精彩的开幕式所吸引,除 Aman外其他人完全忘记了发放 ...

  6. Java缓冲字符读取

    public class BufferedReaderDemo { public static void main(String[] args) throws IOException { // 创建流 ...

  7. php接受post传值的方法

    这段时间在研究php的接口,利用jmeter模拟发送数据给php服务器,看php如何接收post传输过去的数据,遇到了几个问题,经过一番度娘之后终于有所理解,记录一下: 这里记录常用的两种post方式 ...

  8. php怎么获取js的变量值

    使用php做网站的时候,经常需要我们与前端的页面进行交互,有时候我们还需要通过php来获得js变量中的值,这种情况我们可以通过在其中嵌入js代码的方式来获得这个变量. 首先我们创建一个test的php ...

  9. 全文检索 使用最新lucene3.0.3+最新盘古分词 pangu2.4 .net 实例

    开发环境 vs2015 winform 程序 1 首先需要下载对应的DLL 文章后面统一提供程序下载地址 里面都有 2 配置pangu的参数 也可以不配置 采用默认的即可 3 创建索引,将索引存放到本 ...

  10. 后端技术杂谈7:OpenStack的基石KVM

    Qemu,KVM,Virsh傻傻的分不清 本文转载自Itweet的博客 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://gi ...