题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717

Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14880    Accepted Submission(s): 4495

Problem 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.

 
题意:
从a走到b,你可以向前走一步,向后走一步或者走a*2步,求最少多少步
 
 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int v[],t[];
int main()
{
int n,a,b,i,f;
while(scanf("%d%d",&a,&b)!=EOF)
{
memset(t,,sizeof(t));
memset(v,,sizeof(v));
queue<int > q;
q.push(a);
v[a]=;
if(a==b)
{
printf("0\n");
continue;
}
while(!q.empty())
{
int ll=q.front(),r=ll-,l=ll+,z=ll*;
q.pop();
//printf("ll=%d\n",ll);
if(z>=&&z<=&&!v[z])
{
q.push(z);
v[z]=v[ll]+;
// printf("v[z]=%d z=%d\n",v[z],z);
}
if(l>=&&l<=&&!v[l])
{
q.push(l);
v[l]=v[ll]+;
// printf("v[l]=%d l=%d\n",v[l],l);
}
if(r>=&&r<=&&!v[r])
{
q.push(r);
v[r]=v[ll]+;
// printf("v[r]=%d r=%d\n",v[r],r);
} if(l==b||r==b||z==b)
{ break;
} } printf("%d\n",v[b]-); }
return ; }

HDU2717-Catch That Cow (BFS入门)的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  3. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  4. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

  5. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  6. HDU2717 Catch That Cow 【广搜】

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  7. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  8. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  9. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  10. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

随机推荐

  1. C++笔试题总结

    1.C和C++的特点与区别? 答:(1)C语言特点:1.作为一种面向过程的结构化语言,易于调试和维护: 2.表现能力和处理能力极强,可以直接访问内存的物理地址: 3.C语言实现了对硬件的编程操作,也适 ...

  2. C#操作MSMQ

    C#操作MSMQ using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  3. System.Web.Optimization 找不到引用,教你如何解决?

    在vs 2017 创建 BundleConfig 时添加引用 using System.Web.Optimization 是报错 提示未找到 解决方法: 在最下端窗口中写入:Install-Packa ...

  4. 14. Longest Common Prefix C++

    采用纵向遍历,即对第一个字符串,取出第一个字符,检查是否出现在随后每一个字符串中,以此类推.当遍历完成或有一个字符串不符合要求,直接return. class Solution { public: s ...

  5. C++ string::size_type

    从逻辑上讲,size()成员函数应该似乎返回整型数值,但事实上,size操作返回是string::size_type类型的值.string类类型和其他许多库类型都定义了一些配套类型(companion ...

  6. 基于js的CURD插件

    前言: 每个web程序对数据库的创建(Create).更新(Update).读取(Retrieve)和删除(Delete)操作都是必不可少的,于是我决定开发一个基于JavaScript和jQuery框 ...

  7. bootstrap 弹框使用

    首先需要准备bootstrap.css,bootstrap .js  jquery 我这里有写好的下载地址如下: https://pan.baidu.com/s/1miMahXe  秘钥:tgts & ...

  8. 【1】windows下IOS开发基础环境搭建

    一.目的 本文的目的是windows下IOS开发基础环境搭建做了对应的介绍,大家可根据文档步骤进行mac环境部署: 二.安装虚拟机 下载虚拟机安装文件绿色版,点击如下文件安装 获取安装包:       ...

  9. centos 安装 composer

    1 安装 composer       curl -sS https://getcomposer.org/installer | php           2 添加到环境变量       mv co ...

  10. js将接口返回的数据序列化

    <div style={{marginLeft: '80px'}}>                     <pre>                         {th ...