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.

讲解:一农夫追牛,牛很笨,他就在原地等着农夫来抓他,并且这是一条直线,农夫很容易就能找到它的,然而农夫却只有三种选择,退一步,走一步,或者走到当前位置的2倍;于是乎我们可以用搜索来解决;

代码如下:

 #include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
int map[];
struct T
{
int x,step;
};
int dfs(T now)
{queue< T >q;
T end;
q.push(now);
while(!q.empty())
{
end=q.front();
q.pop();
map[end.x]=;
if(end.x==m)
{return end.step;}
//如果不是走一步一判断很容易超出内存的
now.x=end.x+;//前进一步,存入队列;
if(end.x>= && end.x<= && map[now.x]==)
{
now.step=end.step+;
map[now.x]==;
q.push(now);
}
now.x=end.x-;//后退一步
if(end.x>= && end.x<= && map[now.x]==)
{
now.step=end.step+;
map[now.x]==;
q.push(now);
}
now.x=end.x*;//前进2倍的位置
if(end.x>= && end.x<= && map[now.x]==)
{
now.step=end.step+;
map[now.x]==;
q.push(now);
}
}
return ;
}
int main()
{
T now;
while(cin>>n>>m)
{
if(n>=m)//如果n大于m则只能后退了;
{
cout<<n-m<<endl;continue;
}
else
{
memset(map,,sizeof(map));
now.x=n;now.step=;
int mm=dfs(now);
cout<<mm<<endl;
}
}
return ;
}

hdoj 2717 Catch That Cow的更多相关文章

  1. hdoj 2717 Catch That Cow【bfs】

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

  2. Hdoj 2717.Catch That Cow 题解

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

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

  4. 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,最先 ...

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

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

  7. HUD 2717 Catch That Cow

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

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

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

  9. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

随机推荐

  1. Php优化方案

    1.尽量静态化: 如果一个方法能被静态,那就声明它为静态的,速度可提高1/4,甚至我测试的时候,这个提高了近三倍. 当然了,这个测试方法需要在十万级以上次执行,效果才明显. 其实静态方法和非静态方法的 ...

  2. java面试第十七天

    5.0新特性: 泛型: 泛型的形式: <E> <E extends 类型> <E extends Numner&comparator>  类名&接口 ...

  3. vue 笔记二

    vue制作weibo 交互 vue-> 1.0 vue-resource ajax php 服务器环境(node) this.$http.get()/post()/jsonp() this.$h ...

  4. 【Linux】数组与关联数组

    数组 数组的定义: variable=(arg1 arg2 arg3 -) 中间用空格分开,数组的下标从0开始 1.获取下标为n的元素 variable[n] 不存在数组溢出的情况,如果下标n> ...

  5. 虚拟机安装的Winserver 2008 R2系统,宿主机无法ping通主机

    新安装的虚拟机,在虚拟机中安装了Winserver2008 R2系统,网络适配器已经设置为桥接模式(自动) 刚开始的几天很正常,但是过了几天后,发现连接不了了,后来才发现是因为网络问题. 解决方案:修 ...

  6. flume的memeryChannel中transactionCapacity和sink的batchsize需要注意事项

    一. fluem中出现,transactionCapacity查询一下,得出一下这些: 最近在做flume的实时日志收集,用flume默认的配置后,发现不是完全实时的,于是看了一下,原来是memery ...

  7. oracle默认连接数150

    当oracle连接数超过最大的150登录不上 sqlplus / as sysdba   ###报错,提示连接数未释放! ---释放网络连接数 ps -ef |grep ora |grep " ...

  8. Intellij IDEA 设置代码着色

    从visual studio到Eclipse再到Intllij IDEA,其实每个编译器都有自己的风格,但是就美观上来说,个人觉得还是visual studio的代码着色比较美观,exlipse次之, ...

  9. java内存泄露问题

    内存泄露是指一个不再被程序使用的对象或变量还在内存中占用存储空间. 在Java语言中,判断一个内存空间是否符合垃圾回收的标准有两个: ①给对象赋予了空值null,以后再没有使用过: ②给对象赋予了新值 ...

  10. openvpn服务端一键生成windows,macos客户端配置文件

    #!/bin/bash #获取参数 while getopts "n:" opt; do case $opt in n) client_name=$OPTARG ;; \?) ;; ...