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. 阿里开源框架-JarsLink-【JAVA的模块化开发框架】

    JarsLink (原名Titan) 是一个基于JAVA的模块化开发框架,它提供在运行时动态加载模块(一个JAR包).卸载模块和模块间调用的API. 需求背景 应用拆分的多或少都有问题.多则维护成本高 ...

  2. linux中的一些常用命令

    shutdown -h now 现在马上关机 shutdown -r now 现在重新启动 reboot 现在重新启动 su - 如果当前是普通用户,则输入这条命令切换到管理员用户(root),如果要 ...

  3. vue+element ui 滚动加载

    <div id="app"> <div class="infinite-list-wrapper" style="overflow: ...

  4. tar shell zip

    tar          打包后生成的文件名全路径                    要打包的目录 tar -czPf xx/script.tar.gz                      ...

  5. hdu4417 Super Mario (树状数组/分块/主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目大意:给定一个长度为n的序列,有m个询问,每次询问包含l,r,h,即询问区间[l,r]小于等 ...

  6. Car的旅行路线(Floyd+模拟)

    题目地址 贼鸡儿猥琐的一道题 好在数据不毒瘤,而且Floyd就OK了. 这道题的难点在于 建图,也很考验模拟能力,需要十分的有耐心. 建图 题目中告诉了我们一个矩形的三个点 我们在平面直角坐标系中随便 ...

  7. UEditor可以如何直接复制word的图文内容到编辑器中

    如何做到 ueditor批量上传word图片? 1.前端引用代码 <!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN& ...

  8. stack1顺序栈

    顺序栈 #include<iostream> using namespace std; #define increasesize 10 template <class Object& ...

  9. ASP汉字转拼音函数的方法

    <% 'ASP汉字转拼音函数 Set d = CreateObject("Scripting.Dictionary") d.add "a",-20319 ...

  10. The Factor

    The Factor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...