题目:

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. ajax使用json数组------前端往后台发送json数组及后台往前端发送json数组

    1.引子 Json是跨语言数据交流的中间语言,它以键/值对的方式表示数据,这种简单明了的数据类型能被大部分编程语言理解.它也因此是前后端数据交流的主要方式和基础. 2.前端往后台传输json数据 第一 ...

  2. 【收藏】每天更新!全网热门公共BT种子 BitTorrent Tracker 列表合集

    每天更新!全网热门公共 BitTorrent Tracker 列表合集. 该项目仅将全网热门的公共 Tracker 列表制作成合集方便大家使用,无需再一个个导入了~. 「English」(tracke ...

  3. MFC的combox禁止键盘输入

    项目中有个combox的下拉窗控件,鼠标双击总能存在焦点,并且会修改combox显示的文字,网上查了好多资料,都说修改style,可是我的vs2015里没发现有style的属性,后面修改 modal ...

  4. [Algo] 26. Kth Smallest Number In Sorted Matrix

    Given a matrix of size N x M. For each row the elements are sorted in ascending order, and for each ...

  5. python语法基础-文件操作-长期维护

    ###############    python-简单的文件操作  ############### # python中文件的操作 # 文件操作的基本套路 # 1,打开文件,默认是是只读方式打开文件 ...

  6. jeesite 去掉 /a

    1.修改 jeesite.properties文件 adminPath=/a为 adminPath= 2.修改 web.xml文件找到如下设置 <filter-mapping> <f ...

  7. python之golb模块

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

  8. BCM93349DCM 手动升级 Fireware 指导

    PC:Personal Computer(这里用的Win7) CM:Cable MODEM(芯片:BCM93349DCM) 一.预置条件 1.PC上已安装TFTP Server,比如tftpd32: ...

  9. Lua-文件操作

    简单模式和完全模式 简单模式(simple model)拥有一个当前输入文件和一个当前输出文件,并且提供针对这些文件相关的操作. 完全模式(complete model) 使用外部的文件句柄来实现.它 ...

  10. Qt Sleep、QCoreApplication::processEvents()(最佳的平衡:一边发送消息,一边睡眠)

    sleep()//秒 msleep()//毫秒 usleep()//微秒 以前为了模拟鼠标点击用过这些函数,可以让进程中断,今天发现我原来的做法其实不对.这组函数会将你当前的线程/进程变为“睡眠”状态 ...