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. IIS 配置问题

    1 IIS错误需要重新运行配置 重新注册.netframework. 解决方式:cmd   C:\Windows\Microsoft.NET\Framework\v4.0.30319 aspnet_r ...

  2. 陈云川的OPENLDAP系列

    前言 本 来,我应该准备一篇精彩的演说辞,从LDAP应用的方方面面讲起,细数LDAP在各种场合应用的成功案例,大肆渲染LDAP应用的辉煌前景,指出有多少机 构和组织的关键业务是建立在LDAP的基础上的 ...

  3. Windows Phpstrom svn 配置

    网上百度找到的解决方案行不通,就是下图两项都不选中.临时是可以的,但是到了第二天,又不行了. 以下是自己瞎弄的,居然可以了. 第一步:安装TortoiseSVN 1.8.* ,注意安装选项要选上com ...

  4. git 报错:没有权限 remote: error: unable to unlink old 'README.md' (Permission denied)

    解决:

  5. 你真的了解HTML吗

    有这么一段HTML,请挑毛病: <P> 哥写的不是HTML,是寂寞.<br><br> 我说: <br>不要迷恋哥,哥只是一个传说 这是原来雅虎一道笔试题 ...

  6. 面试问题:Vuejs如何实现双向绑定

    最近出去面试,栽在这个问题上,提到vuejs,面试官一般会让你说vuejs的特点,一般就要回答virtual dom tree, dom tree diff, 以及数据双向绑定,然后面试官会追问你,v ...

  7. UIImageView添加圆角

    最直接的方法就是使用如下属性设置: 1 2 3 imgView.layer.cornerRadius = 10; // 这一行代码是很消耗性能的 imgView.clipsToBounds = YES ...

  8. MySQL数据库操作语句(补充1)(cmd环境运行)

    一.字符串类型 enum枚举类型 /* 也叫做枚举类型,类似于单选! 如果某个字段的值只能从某几个确定的值中进行选择,一般就使用enum类型, 在定义的时候需要将该字段所有可能的选项都罗列出来: */ ...

  9. sqlserver 带返回值的存储过程

    create proc test ) output as begin select @result = 'haha' ; end go ), @count int exec @count = test ...

  10. 一个iOS开发者对tvOS SDK的初探

    http://www.cocoachina.com/ios/20151001/13652.html 作者:Chris Wagner原文地址:tvOS SDK: An iOS Developer’s I ...