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. 线程池ThreadPoolExecutor实现原理

    线程属于稀缺资源,对于线程的创建规则,引用<阿里巴巴 Java 手册>中的一条进行说明. 本篇从源码方面介绍ThreadPoolExecutor对象,并简要解析线程池工作原理. 首先Thr ...

  2. python面向对象--类的内置函数

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  3. md5sum 计算和校验文件的md5值

    1. 命令功能 md5算法一般用于检查文件完整性, 2. 语法格式 md5sum  [option]  [file] 参数 参数说明 -b 以二进制模式读入文件 -t 以文本模式读入文件 -c 用来从 ...

  4. centos6 安装mysql5.77(开发版)

    1. 配置yum源: [root@yyf ~]#rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm [ ...

  5. Eclipse使用maven创建SSM-web项目

    1.环境准备 (1)maven:apache-maven-3.5.2,我的maven安装在D:\apache-maven-3.5.2,eclipse中maven配置如下: 先add ,添加好自己的ma ...

  6. Cluster基础(三):配置HAProxy负载平衡集群、Keepalived高可用服务器、Keepalived+LVS服务器

    一.配置HAProxy负载平衡集群 目标: 准备三台Linux服务器,两台做Web服务器,一台安装HAProxy,实现如下功能: 客户端访问HAProxy,HAProxy分发请求到后端Real Ser ...

  7. C# 调用delphi编写的dll

    技术实现 如何逐步实现动态库的加载,类型的匹配,动态链接库函数导出的定义,参考下面宏定义即可: #define LIBEXPORT_API extern "C" __declspe ...

  8. MySQL:MySQL日期数据类型、MySQL时间类型使用总结

    MySQL 日期类型:日期格式.所占存储空间.日期范围 比较. 日期类型        存储空间      日期格式                日期范围------------  -------- ...

  9. HDU4405 Aeroplane chess (概率DP,转移)

    http://acm.hdu.edu.cn/showproblem.php?pid=4405 题意:在一个1×n的格子上掷色子,从0点出发,掷了多少前进几步,同时有些格点直接相连,即若a,b相连,当落 ...

  10. HDU6672 Seq(找规律)

    HDU6672 Seq 对于递推式 \(a_n = (\sum_{i = 1}^{n-1}a_i * i)\%n\) 打表列出 \(a_i\) 的前 \(100\) 项,发现有以 \(6\) 为循环的 ...