Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9753    Accepted Submission(s): 3054

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.

 



Source
 



Recommend
teddy   |   We have carefully selected several similar problems for you:  1372 1072 1180 1728 1254 
 
 
一道很简单的一维广搜题,将每次坐标变化存入队列,标记不重复即可。
 
题意:一个农民去抓一头牛,输入分别为农民和牛的坐标,农民每次的移动可以坐标+1,或者坐标-1,或者坐标乘2三种变化,假设牛不知道农民来抓它而一直呆在原地不动,农民最少需要几步才能抓到牛。
 
附上代码:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define M 100005
using namespace std;
int n,m;
int visit[M]; //标记数组,0表示没走过,1表示已走过
struct node
{
int x,t;
} s1,s2;
void BFS()
{
queue<node> q;
while(!q.empty())
q.pop();
s1.x=n;
s1.t=;
visit[n]=; //农民起始位置标记为已走过
q.push(s1);
while(!q.empty())
{
s1=q.front();
q.pop();
if(s1.x==m) //结束标志为农民到了牛的位置
{
printf("%d\n",s1.t);
return;
}
s2.x=s1.x+; //坐标+1
if(s2.x>=&&s2.x<=M&&!visit[s2.x]) //判断变化后的数字是否超过了范围
{
visit[s2.x]=;
s2.t=s1.t+;
q.push(s2);
}
s2.x=s1.x-; //坐标-1
if(s2.x>=&&s2.x<=M&&!visit[s2.x])
{
visit[s2.x]=;
s2.t=s1.t+;
q.push(s2);
}
s2.x=s1.x*; //坐标*2
if(s2.x>=&&s2.x<=M&&!visit[s2.x])
{
visit[s2.x]=;
s2.t=s1.t+;
q.push(s2);
}
}
}
int main()
{
int i,j;
while(~scanf("%d %d",&n,&m))
{
memset(visit,,sizeof(visit)); //开始全部定义为0
BFS();
}
return ;
}

poj 3278(hdu 2717) Catch That Cow(bfs)的更多相关文章

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

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  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. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  5. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  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. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

  8. POJ 3278 Catch That Cow(求助大佬)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 109702   Accepted: 34255 ...

  9. Catch That Cow(BFS)

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

随机推荐

  1. js实现HashTable

    1.哈希表使用键值对进行的数据储存,在数据的存储位置和它的关键字之间建立一一对应的关系,从而使关键字和结构中的一个唯一的存储位置相对应,所以在检索数据时 只需要根据这个关系便可以快速定位到要找的数据. ...

  2. Laravel5.5/6 报错call to undefined function openssl cipher iv length()

    在安装laravel5.5后, 访问显示报错. call to undefined function openssl cipher iv length() 经查为php7.1的OpenSSL扩展加载失 ...

  3. 【JZOJ3601】【广州市选2014】Tree(tree)

    ╰( ̄▽ ̄)╭ 每个非叶子节点,其左右子树叶子节点的权值之和相等.我们称这种二叉树叫平衡二叉树. 我们将一棵平衡二叉树叶子节点的权值从左到右列出来,假如这个权值序列是另一个序列A的子序列,我们称这棵平 ...

  4. ChaosBlade 发布对 C++ 应用混沌实验的支持

    前言 为满足 C++ 应用系统故障演练,阿里妈妈安全生产团队开源了 C++ 混沌实验执行器,填补了 C++ 应用混沌工程实验的空白,其遵循<混沌实验模型>,可通过 ChaosBlade 工 ...

  5. 安装tomcat(fedora16)

    sudo yum install tomcat6 sudo yum install tomcat6-webapps sudo yum install tomcat6-admin-webapps   s ...

  6. OFBiz 16.11.03的直接部署、eclipse部署和IDEA部署

    一.在OFBiz官网下载最新的发行版本,也就是16.11.03版本. 下载地址:http://ofbiz.apache.org/download.html   点击页面Apache OFBiz 16. ...

  7. Mysql里查询字段为Json格式的数据模糊查询以及分页方法

    public void datagrid(CustomFormEntity customForm,HttpServletRequest request, HttpServletResponse res ...

  8. 如何用好消息推送(push)做APP运营

    作为移动端APP产品运营最重要的运营手段,消息推送(push)被越来越多的APP厂商所重视,在信息泛滥的移动互联网时代,手机APP应用安装得越来越多,小小的手机屏幕每天收到的消息推送也越来越多,站在用 ...

  9. Linux常用命令1 文件处理命令

    1.命令格式 1.用中括号括起来的内容都不是必填内容,碧如上图的选项和参数,有些命令不写选项和参数也可以执行 2.注意图中的简化选项与完整选项说明,完整选项要两个横杆-- 2.目录处理命令ls 1.文 ...

  10. Please upgrade the installed version of powershell to the minimum required version and run the command again.

    版权声明:本文为博主原创文章,转载请注明出处.谢谢 https://blog.csdn.net/cow66/article/details/77993908 我的系统是windows 7 安装了vag ...