题目传送门: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. PyCharm + PyQt4 环境搭建

    一.准备工作 下载pycharm 和 pyqt4 (百度下就有) pyqt4安装好后,在C:\Python27\Lib\site-packages\PyQt4 路径下会有designer.exe ,这 ...

  2. hpu_newoj_1028-exgcd

    The Elevator   描述 全是电梯. Philo正处于高度为0的一个平台上,在他面前的一个平面,全是上上下下的电梯. Philo想要离开这里,请你帮帮他. 电梯世界规则:这里的电梯所能到达的 ...

  3. json 常用的序列化 反序列化对象 代码

    序列化对象: ---------------------------------------------------------- Person p = new Person() { Name = & ...

  4. github上fork了别人的项目后,再同步更新别人的提交(转)

    原文地址:github上fork了别人的项目后,再同步更新别人的提交 我从github网站和用git命令两种方式说一下. github网站上操作 打开自己的仓库,进入code下面. 点击new pul ...

  5. 洛谷P1075 质因数分解

    题目描述 已知正整数n是两个不同的质数的乘积,试求出两者中较大的那个质数. 输入输出格式 输入格式: 一个正整数n. 输出格式: 一个正整数p,即较大的那个质数. 输入输出样例 输入样例#1: 复制 ...

  6. Android AES加密报错处理:javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH

    一.问题说明 今天写AES加/解密功能的apk,设想是四个控件(测试用的,界面丑这种东西请忽略) 一个编缉框----用于输入要加密的字符串 一个文本框----用于输出加密后的字符串,和加密后点击解密按 ...

  7. redis集群搭建教程(以3.2.2为例)

    redis从3.0版本开始支持集群,2.X版本主支持sentinel主从模式:所以要搭建集群务必下载3.0以上版本,本教程以3.2.2版本为例. redis集群最少要有3个主节点,最典型的是3主3从组 ...

  8. telnet的装配及xinetd的讨论

    telnet由于是不安全的明文传输所以基本被ssh取代了,尤其是在Linux上:不过还是可能会用到,且启停方式还有些不同所以还是有必要说明一下. rpm -qa | grep telnet #查看是否 ...

  9. Python isspace() 方法检测字符串是否只由空格组成。

  10. 把旧系统迁移到.Net Core 2.0 日记 (15) --Session 改用Redis

    安装Microsoft.Extensions.Caching.Redis.Core NuGet中搜索Microsoft.Extensions.Caching.Redis.Core并安装,此NuGet包 ...