Catch That Cow



Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8764    Accepted Submission(s): 2762



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

//此题是在一条水平线上追赶羊。分为三种情况 x-1 /x+1 /2*x   注意边界就可以

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n,start,stop;
int p[210];
int vis[210];
struct Node
{
int floor;
int step;
}; int bfs(int flo)
{
queue <Node> q;
memset(vis,0,sizeof(vis));
Node a;
a.floor=flo,a.step=0;
q.push(a);
while(!q.empty())
{
Node b=q.front();
q.pop();
vis[b.floor]=1;
if(b.floor==stop) return b.step;
for(int i=0;i<2;i++) //0 up 1 down
{
Node c=b;
if(i==0)
c.floor=c.floor+p[c.floor];
else
c.floor=c.floor-p[c.floor]; if(!vis[c.floor]&&c.floor>=1&&c.floor<=n)
{
c.step++;
q.push(c);
vis[c.floor]=1;
}
}
}
return -1;
} int main()
{
while(~scanf("%d",&n)&&n)
{
scanf("%d%d",&start,&stop);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("%d\n",bfs(start));
}
return 0;
}

HDU 2717的更多相关文章

  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. HDU 2717 Catch That Cow (bfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...

  3. HDOJ/HDU 2717 Catch That Cow 一维广度优先搜索 so easy..............

    看题:http://acm.hdu.edu.cn/showproblem.php?pid=2717 思路:相当于每次有三个方向,加1,减1,乘2,要注意边界条件,减1不能小于0,乘2不能超过最大值. ...

  4. HDU 2717 Catch That Cow(常规bfs)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Oth ...

  5. HDU 2717 Catch That Cow(BFS)

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

  6. hdu 2717 Catch That Cow(BFS,剪枝)

    题目 #include<stdio.h> #include<string.h> #include<queue> #include<algorithm> ...

  7. HDU 2717 Catch That Cow

    简单的广搜: #include <cstdio> #include <queue> using namespace std; ],step[]; int n,start,end ...

  8. HDU 2717(* bfs)

    题意是在一个数轴上,每次可以一步到达当前位置数值的 2 倍的位置或者数值 +1 或数值 -1 的位置,给定 n 和 k,问从数值为 n 的位置最少多少步可以到达数值为 k 的位置. 用广搜的方法,把已 ...

  9. hdu 2717 从n点走到k点 (BFS)

    在横坐标上 从n点走到k点 至少要几步 可以到 n+1 n-1 n*2这3个点 Sample Input5 17 Sample Output4 #include <iostream> #i ...

  10. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

随机推荐

  1. Python 45 css三种引入方式以及优先级

    一:css三种引入方式 三种方式为:行间式 | 内联式 | 外联式 行间式   1.在标签头部的style属性内  2.属性值满足的是css语法  3.属性值用key:value形式赋值,value具 ...

  2. Python 34(进程重点)

    一:开启进程的两种方式(*****) #开启进程的方式一: from multiprocessing import Process import time def task(name): print( ...

  3. Stick ------ 剪枝神题

    这个是自己剪得 , 我感觉已经很不错了  但是不知道哪里出了问题  一直  超时 // 根据所给答案 和 题目要求 最直观的就可以有剪枝的地方 而且 剪枝剪得越早 就越省时省力 // 好的思路也可以省 ...

  4. C#开发微信公众号——网页开发之微信网页授权

    首先咱们先看下公众号的文档里面的介绍 上述图片的文字描述就是讲述了网页授权有什么用,就是为了获取微信用户的基本信息:授权回调域名的规范,说到域名回调的事情就不得不提一下设置网页授权域名 最好将这三个域 ...

  5. 5.14JDBC

    一.##JDBC 1. 概念:Java DataBase Connectivity  Java 数据库连接, Java语言操作数据库. JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数 ...

  6. Android 判断app是否安装

    1. private boolean isAppInstalled(Context context, String uri) { PackageManager pm = context.getPack ...

  7. 基于CXF搭建webService

    1.导入相关jar包,具体哪些包我记不太清了 2.在applicationContext中加入相关配置信息,如下所示: <beans xmlns="http://www.springf ...

  8. spring - quartz - experssion 表达式

    字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN- ...

  9. 【SQL】MERGE

    MERGE可以合并多个表中的数据,也可实现多表中数据的同步.使用MERGE语句对表中数据进行有条件的更新和插入.当查找的行存在时,UPDATE更新行中的数据:当查找的行不存在时,INSERT插入数据. ...

  10. mysql安装包下载地址

    1.打开mysql官网 :https://dev.mysql.com/ 2.选择 downlad-->windows-->MySQL Installer -->滑动至页面底部,选择第 ...