个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时;

将每个动作扔入队列,但要注意如何更简便,更节省时间,空间

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.
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int sum;
int ok=;
struct Node
{
int x;
int y; };
int book[]; void dfs(int n,int m)
{
memset(book,,sizeof(book));
queue<Node >s;
book[n]=;
Node t;
t.x=n;t.y=;
s.push(t);
int a;
Node tt;
while(!s.empty())
{
a=s.front().x*;
tt.x=a,tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); }
a=s.front().x+;
tt.x=a;
tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); };
a=s.front().x-;
tt.x=a,tt.y=s.front().y+;
if(a==m)
{
sum=tt.y;
return ; }
if(tt.x>=&&tt.x<=)
if(!book[a])
{
book[a]=;
s.push(tt); }
s.pop(); }
return ; }
int main()
{ int n,m;
while(cin>>n>>m)
{
sum=;
if(n>=m) sum=n-m;
else
dfs(n,m);
cout<<sum<<endl; }
return ; }

Catch That Cow(广搜)的更多相关文章

  1. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

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

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

  3. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

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

  4. Catch That Cow(BFS广搜)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  5. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  6. HDU2717 Catch That Cow 【广搜】

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

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

  8. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

  9. 广搜 poj3278 poj1426 poj3126

    Catch That Cow Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Ja ...

随机推荐

  1. pyhton3 random模块

    random是内建(built-in)函数,作用是产生随机数 导入模块: import random 接着就可以调用random模块下的函数了使用 dir(random)可以查看random模块下有哪 ...

  2. linux 网卡buffer大小

    参考截取一部分:https://blog.csdn.net/ysu108/article/details/7764461 在linux下可以修改协议栈改变tcp缓冲相关参数: 修改系统套接字缓冲区 e ...

  3. nand flash详解及驱动编写

    https://www.crifan.com/files/doc/docbook/linux_nand_driver/release/html/linux_nand_driver.html#nand_ ...

  4. @MarkFan 口语练习录音 20140406 [美女与野兽的口语练习录音]

    大家好,您现在收听的是美女与野兽的口语练习录音 敢于追求,不惧任何挑战,才是勇敢的人生.试想一下,世界上每天有多少人为了梦想,为了生活,甚至是为了别人在不停地奔跑.假若你此刻心中装有梦想,却碍于现实不 ...

  5. awk中使用shell变量

    方法:使用-v参数. 对于多个shell变量使用多个-v 有个关于shell变量中的空格问题:

  6. get_class_methods--返回由类的方法名组成的数组

    get_class_methods--返回由类的方法名组成的数组 array get_class_methods ( mixed $class_name ) 返回由类的方法名组成的数组. <?p ...

  7. 七 、linux正则表达式

    为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...

  8. Kubernetes addon-manager

    Addon-manager 附加组件管理器(Addon-manager)是运行在 Kubernetes 集群 Master 节点.用来管理附加组件(Addons)的服务.它管理着 $ADDON_PAT ...

  9. JMeter学习(一)目录介绍

    JMeter也学了一阵子了,对于基本的操作已了解,再回过头来看看Jmeter的目录,本篇是对于它的目录进行一些简单的介绍. JMeter解压之后打开,根目录如下图: 1.bin:可执行文件目录 2.d ...

  10. linux 上传scp 压缩tar命令

    1.Linux 上传scp 1)上传文件与文件夹 scp file weblogic@xx.xxx.xxx.xxx:~/songjd/ scp -r filefolder weblogic@xxx.x ...