题目传送门: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. MSMQ消息传递的优先级

    一.消息传递的优先级 在MSMQ中消息在队列里传输是分有优先级的,这里我就以实例的形式介绍下关于优先级的使用,优先级一共有七种,MessagePriority枚举里全部进行了封装.因这里只作程序演示就 ...

  2. Connection parameters are correct , SSL not enabled

    这个仅仅是个消息提示,告诉你SSL not enabled.无须理会,直接点击ok

  3. ORA-01034: ORACLE not available ORA-27101

    出现ORA-01034和ORA-27101的原因是多方面的:主要是oracle当前的服务不可用,shared memory realm does not exist,是因为oracle没有启动或没有正 ...

  4. C++ leetcode::ZigZag Conversion

    mmp,写完没保存,又得重新写.晚上写了简历,感觉身体被掏空,大学两年半所经历的事,一张A4纸都写不满,真是一事无成呢.这操蛋的生活到底想对我这个小猫咪做什么. 今后要做一个早起的好宝宝~晚起就诅咒自 ...

  5. Humble Numbers HDU - 1058 2分dp

    JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two ...

  6. Python购物车

    product_list = [ ['Iphone',5888], ['Mac Air',8000], ['XiaoMi',19.9], ['coffee',30], ['Tesla',820000] ...

  7. openssh允许root用户登录

    openssh默认是不允许root用户登录的,未改配置直接以正确的root账号密码登录会提示用户名密码错误之类. 好消息是openssh配置十分清晰,基本所有的服务端配置都在/etc/ssh/sshd ...

  8. 快捷键设置 keyiing.json

    // 快捷键设置 keyiing.json // 将键绑定放入此文件中以覆盖默认值 [     /* // 转换大写     {         "key" : "ctr ...

  9. memory prefix il ir im in out 3 i

    1● il   2● ir 不 非 无 :使 ~ 成为:   3● im 4● in 不 非 无 :向内,进入  

  10. JS&Jquery中的循环/遍历

    JavaScript中的遍历 for 遍历 var anArray = ['one','two']; for(var n = 0; n < anArray.length; n++) { //具体 ...