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. TCP相关面试题总结

    1.TCP三次握手过程 wireshark抓包为:(wireshark会将seq序号和ACK自己主动显示为相对值) 1)主机A发送标志syn=1,随机产生seq =1234567的数据包到server ...

  2. mavern安装方法

    Installation Instructions Maven is a Java tool, so you must have Java installed in order to proceed. ...

  3. MySQL的各种SHOW

    . SHOW语法 13.5.4.1. SHOW CHARACTER SET语法 13.5.4.2. SHOW COLLATION语法 13.5.4.3. SHOW COLUMNS语法 13.5.4.4 ...

  4. pushlet单播与多播

    近期要弄一个消息推送的功能,在网上找了非常多的关于pushlet的文章,尽管写的都非常具体,可是本人看了以后却总认为是模棱两可···不知道怎样下手,终于參考了这些文章中的一些内容,并结合官网的源码.做 ...

  5. poj 4014 Dice 贪心

    //poj 4014 //sep9 #include <iostream> #include <algorithm> using namespace std; int n; s ...

  6. google打不开解决的方法

    14.5.27以来.谷歌又打不开了. 从网上找了些国内的googleserverIP,例如以下: const char* g_google_ips[18] = { "203.208.48.1 ...

  7. c# 判断端口是否被占用

    这里主要用到:命名空间System.Net.NetworkInformation下定义了一个名为IPGlobalProperties的类 具体代码 class PortHelper { #region ...

  8. EditPlus破解版|很不错的编辑器

    Windows自带的notepad,功能一般.editplus,很值得推荐的编辑器! 注册秘钥: 用户名:www.dayanzai.me序列号:D2C86-E97A0-BF376-FDC33-27C1 ...

  9. SDK Build Tools revision (19.0.3) is too low for project Minimum required is 19.1.0

    假设你正在使用Android Studio工具进行开发,且将版本号更新到0.6.0的时候.莫名的出现这种错误 SDK Build Tools revision (19.0.3) is too low ...

  10. 使用增强for循环遍历集合的时候操作集合的问题?

    // 遍历一个list public static void printList(List<String> list){ for (String string : list) { list ...