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. sql将一个表中的数据插入到另一个表中

    sql将一个表中的数据插入到另一个表中 列名不一定要相同,只要你在HH中列出要插入列的列表跟select   from   mm表中的选择的列的列表一一对应就可以了,当然两边的数据类型应该是兼容的.  ...

  2. 关于sublime代码格式化

    就我接触到的,html,css,js,json,php语言来介绍. html,css,json,js这些,我感觉pretty是比较好用的,ctrl+shift+h快捷键,给人不一样的感受,不过这个插件 ...

  3. POJ 3083 BFS+DFS 40行

    题意:给你一个迷宫. 先输出当左转优先的时候走的路程长度,再输出当右转优先时走的路程长度,最后输出从起点到终点的最短路程长度. 嗯嗯 奴哥活跃气氛的题.随便写了写.. 此题 知道了思路以后就是水题了. ...

  4. POJ 1118 求平面上最多x点共线

    题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...

  5. A - HQ9+

    Problem description HQ9+ is a joke programming language which has only four one-character instructio ...

  6. vs2008bin下Debug bll Release文件 obj下的Debug bll Release文件区别

    Bin目录用来存放编译的结果,bin是二进制binrary的英文缩写,因为最初C编译的程序文件都是二进制文件,它有Debug和Release两个版本,分别对应的文件夹为bin/Debug和bin/Re ...

  7. SQLServer2008 将本地excel导入到远程服务器表

    --1.创建链接服务器,相当于创建一个访问远程数据库的快捷方式 exec sp_addlinkedserver 'TestLink', ' ', 'SQLOLEDB ', '111.11.1.111' ...

  8. Android Retrofit 2.0文件上传

    Android Retrofit 实现(图文上传)文字(参数)和多张图片一起上传 使用Retrofit进行文件上传,肯定离不开Part & PartMap. public interface ...

  9. [WIFI插座][阅读记录][SoC][RT5350] 00.目录 RALINK AP SDK 4.1.0.0 USER’s MANUAL

    来源是CSDN,百度网盘下载地址 http://pan.baidu.com/share/link?shareid=3504767505&uk=3426044377   授权声明,略过 免责声明 ...

  10. ubuntu 14.04安装x11VNC

    环境:Ubuntu 14.04, 1)安装x11vnc: sudo apt-get install x11vnc 2)设置VNC的连接密码: x11vnc -storepasswd Enter VNC ...