题目:

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?InputLine 1: Two space-separated integers: N and KOutputLine 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.

题意:
人抓牛,数轴上人在点N处,牛在点K处,通过前进1,后退1,当前位置乘以2三种方式到达K处,牛在K处不动,求人从N处到牛的K处所经过的最少步数; 分析:
求最小路径,用BFS;
分为+1,-1,*2三种情况;
分为N在K之前或者在K上的情况和N在K之后的情况,N在K之前就只能通过-1的操作到达K
定义一个数组用于标记经过的位置坐标;
定义一个数组用于记录到达当前位置所需要的最少步数;
在三种情况下,满足范围即入队列,对当前的坐标进行标记,步数加1; AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int Max=;
int n,k;
int f[Max];
int step[Max];
bool cmp(int x)
{
return (x>=&&x<=Max);
}
int a,b;
void bfs()
{
queue<int>s;
s.push(n);
f[n]=;
step[n]=;
while (!s.empty())
{
a=s.front();
s.pop();
if (a==k)
break;
b=a-;
if (cmp(b)&&!f[b])
{ s.push(b);
f[b]=;
step[b]=step[a]+;
}
b=a+;
if (cmp(b)&&!f[b])
{
s.push(b);
f[b]=;
step[b]=step[a]+;
}
b=a*;
if (cmp(b)&&!f[b])
{
s.push(b);
f[b]=;
step[b]=step[a]+;
} }
}
int main()
{ while (scanf("%d %d",&n,&k)==)
{
memset(f,,sizeof(f));
if (n>=k)
printf("%d\n",n-k);
else
{
bfs() ;
printf("%d\n",step[k]);
}
} return ;
}



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

  2. POJ 3278 Catch That Cow(bfs)

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

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

  4. Catch That Cow(BFS)

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

  5. ***参考Catch That Cow(BFS)

    Catch That Cow Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tot ...

  6. Catch That Cow (bfs)

    Catch That Cow bfs代码 #include<cstdio> #include<cstring> #include<algorithm> #inclu ...

  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. 【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)

    Catch That Cow Descriptions: Farmer John has been informed of the location of a fugitive cow and wan ...

  9. poj 3278(hdu 2717) Catch That Cow(bfs)

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

  10. POJ3279 Catch That Cow(BFS)

    本文来源于:http://blog.csdn.net/svitter 意甲冠军:给你一个数字n, 一个数字k.分别代表主人的位置和奶牛的位置,主任能够移动的方案有x+1, x-1, 2*x.求主人找到 ...

随机推荐

  1. 小白学习之pytorch框架(5)-多层感知机(MLP)-(tensor、variable、计算图、ReLU()、sigmoid()、tanh())

    先记录一下一开始学习torch时未曾记录(也未好好弄懂哈)导致又忘记了的tensor.variable.计算图 计算图 计算图直白的来说,就是数学公式(也叫模型)用图表示,这个图即计算图.借用 htt ...

  2. upstream实现内网网站在公网访问

    背景描述:公司内网有个网站aa.com,B部门需要访问这个aa.com,但是网站部署在内网服务器(服务器是192.168.1网段),B部门网段是192.168.100 需求描述:B部门需要访问aa.c ...

  3. LeetCode——264. 丑数 II

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  4. D - Daydreaming Stockbroker Gym - 101550D

    题目链接:http://codeforces.com/gym/101550/attachments 总的来说就是要: 极大值卖出,极小值买入, 再加上端点时的特判. 还有就是会有连续几天股票价格相同的 ...

  5. RSAUtils加密解密

    import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import javax.cr ...

  6. grep 提取百度网盘的链接

    弄到一堆学习资料,都是网盘地址,其中有很多失效了,不想一个个试 3.3第20季:HTML5特效实战 https://pan.baidu.com/s/1kVBrpZp 3.4第21季:3小时玩转微信小程 ...

  7. 【Java杂货铺】JVM#虚拟机加载机制

    代码编译的结果从本地机器码变为字节码,是储存格式发展的一小步,却是编程语言发展的一大步--<深入理解Java虚拟机> 虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转化 ...

  8. JQ和JS的等价代码

    JQ与JS等价代码   选择器 //jquery var els = $(".el"); //原生方法 var els = document.querySelectorAll(&q ...

  9. [Usaco2009 Oct]Heat Wave 热浪(裸最短路径)

    链接:https://ac.nowcoder.com/acm/contest/1082/F来源:牛客网 题目描述 The good folks in Texas are having a heatwa ...

  10. python之golb模块

    golb模块可以查找符合特定规则的文件路径名,查找文件名使用三种不同的匹配符:‘*’,‘?’,‘[]’.'*'匹配0个或多个字符,'?‘匹配单个字符,’[]‘匹配指定范围内的字符,比如[A-Z] 1. ...