Catch That Cow

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

Total Submission(s): 8999    Accepted Submission(s): 2837

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. 睡前一水~ 好久没做过搜索了 注意剪枝&&&&&&&&
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
struct Node
{
int x,step;
Node(){}
Node(int x,int step):x(x),step(step){}
}; int n,k;
bool vis[199999]; int bfs()
{
queue<Node>Q;
Node cur,next;
Q.push(Node(n,0));
while(!Q.empty())
{
cur=Q.front();
Q.pop();
for(int i=0;i<3;i++)
{
if(i==0)
next.x=cur.x+1;
if(i==1)
next.x=cur.x-1;
if(i==2)
next.x=cur.x*2;
next.step=cur.step+1;
if(next.x==k)
return next.step;
if(next.x<0||next.x>100000)
continue;
if(!vis[next.x])
{
vis[next.x]=true;
Q.push(next);
}
}
} } int main()
{
while(cin>>n>>k)
{
memset(vis,0,sizeof(vis));
if(n<k)
cout<<bfs()<<endl;
if(n==k)
cout<<0<<endl;
if(n>k)
cout<<n-k<<endl;
}
return 0;
}

杭电 HDU 2717 Catch That Cow的更多相关文章

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

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

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

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

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

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

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

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

  8. HDU 2717 Catch That Cow (深搜)

    题目链接 Problem Description Farmer John has been informed of the location of a fugitive cow and wants t ...

  9. 题解报告:hdu 2717 Catch That Cow(bfs)

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

随机推荐

  1. shortcut(NOIP模拟赛)(裸的排序)

    原题: Description Mirek有一条每天从他家去大学工作的最喜欢的路.这个路径由若干个部分组成,且每个部分是10米长的直线.每一个部分是直线连接(没有拐弯)上一个部分或垂直连接上一个部分. ...

  2. wxpython demo

    #!/usr/bin/python # encoding: utf-8 '''Spare.py is a starting point for a wxPython program.''' impor ...

  3. Centos 环境变量

    1. 控制台中,不赞成使用这种方法,因为换个shell,你的设置就无效了,因此这种方法仅仅是临时使用,以后要使用的时候又要重新设置,比较麻烦. 这个只针对特定的shell; $ PATH=" ...

  4. 【推荐】nodeJS后台守护进程-forever

    A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever) 本地执行: npm i ...

  5. super真的是调用父类吗?

    #!/usr/bin/env python # -*- coding:utf-8 -*- # author:love_cat class A: def __init__(self): print(&q ...

  6. springBoot Feign Hystrix

    1.引入依赖包 <!-- 引入关于 hystrix的依赖 --> <dependency> <groupId>org.springframework.cloud&l ...

  7. React+dva.js+typescript实现百度贴吧移动web端

    个人练习作品,有bug欢迎在github上提:) github地址:https://github.com/axel10/react-tieba 整个项目中实现起来最麻烦的应该算是滚动位置记忆和路由动画 ...

  8. Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】

    C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Cookie和Session在Node.JS中的实践(二)

    Cookie和Session在Node.JS中的实践(二) cookie篇在作者的上一篇文章Cookie和Session在Node.JS中的实践(一)已经是写得算是比较详细了,有兴趣可以翻看,这篇是s ...

  10. nginx的proxy_set_header

    nginx配置的server段: listen 8888; server_name wyc.com; location /user { proxy_set_header HOST fake.com; ...