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. load()和get()的区别

    区别1:如果数据库中,没有userId的对象.如果通过get方法加载,则返回的是一个null:如果通过load加载,则返回一个代理对象,如果后面代码如果调用user对象的某个属性(比如user.get ...

  2. samba温故知新

    SAMBA服务器可以实现Windows主机和Linux主机共享资源互访的功能,即在Windows下可以通过网上邻居访问Linux操作系统中SAMBA服务器共享的文件夹,当然,Linux操作系统之间同样 ...

  3. leetcode || 58、Length of Last Word

    problem: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ret ...

  4. 转:PHP关于反斜杠处理函数addslashes()和stripslashes()的用法

    1.php处理\函数:addslashes()和stripslashes()函数 addslashes():对输入字符串中的某些预定义字符前添加反斜杠,这样处理是为了数据库查询语句等的需要.这些预定义 ...

  5. kubectl命令使用

    语法:   kubectl  [command]  [TYPE] [NAME]  [flags]   1 command:子命令,用于操作Kubernetes集群资源对象的命令,如create, de ...

  6. HDUOJ----Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. HDUOJ----(1016)Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  9. Sublime Text 2搭建Go开发环境(Windows)

    转自:http://blog.csdn.net/love_se/article/details/7754274 下载packcontrol包地址:http://www.imjeff.cn/blog/6 ...

  10. JAXB注解使用[转]

    一.Jaxb处理java对象和xml之间转换常用的annotation有: @XmlType @XmlElement @XmlRootElement @XmlAttribute @XmlAccesso ...