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. MyBatis报错 Parameter '0' not found. Available parameters are [arg1, arg0, param1, param2]

    修改 <update id="updateStatusById" parameterType="java.lang.Integer"> update ...

  2. css容器

    .s1{ background-color:pink; font-weight:bold; font-size=16px; color:black; } #id1{ background-color: ...

  3. 使用bottle进行web开发(2):http request

    我们知道,http request有多个方法,比如get,post,delete,patch,put等.对用的,bottle都定义了相应的装饰器,目前定义了五个: get(),post(),put() ...

  4. Hmailserver搭建邮件服务器

    Hmailserver安装很简单不需要多说,自己去百度 Hmailserver配置: 输入安装时设置的密码登录Hmailserver 添加域名,如:域名是:mail.abc.com这里添加的时候应该填 ...

  5. HDU 2191 【多重背包】

    Input 输入数据首先包含一个正整数C,表示有C组测试用例,每组测试用例的第一行是两个整数n和m(1<=n<=100, 1<=m<=100),分别表示经费的金额和大米的种类, ...

  6. Codeforces Round #446 (Div. 2) C. Pride【】

    C. Pride time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  7. luogu P3834 【模板】可持久化线段树 1(主席树)

    题解真的是越写越懒 // luogu-judger-enable-o2 #include<cstdio> #include<algorithm> using std::sort ...

  8. luogu P1284 三角形牧场

    题目描述 和所有人一样,奶牛喜欢变化.它们正在设想新造型的牧场.奶牛建筑师Hei想建造围有漂亮白色栅栏的三角形牧场.她拥有N(3≤N≤40)块木板,每块的长度Li(1≤Li≤40)都是整数,她想用所有 ...

  9. [P2023][AHOI2009]维护序列(线段树)

    题目描述 老师交给小可可一个维护数列的任务,现在小可可希望你来帮他完成. 有长为N的数列,不妨设为a1,a2,…,aN .有如下三种操作形式: (1)把数列中的一段数全部乘一个值; (2)把数列中的一 ...

  10. Chrome下强制http重定向到https的问题

    问题: Chrome会强制将http重定向到https,就算是在浏览器手动输入http://xxx也不可以. 解决方案: 1. 在chrome的地址栏输入chrome://net-internals/ ...