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

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

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. node+npm安裝配置

    控制臺輸入node 根據提示安裝   sudo apt-get install -g npm配置淘寶源 npm config set registry https://registry.npm.tao ...

  2. Linux用户和用户组管理 用户组管理命令

    添加用户组命令:groupadd 命令格式: [root@localhost ~]# groupadd [选项] 组名 选项: 选项 选项说明 -g GID 指定组ID: 修改用户组命令:groupm ...

  3. 【leetcode刷题笔记】Wildcard Matching

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  4. JAVAWeb学习总结(二)

    JavaWeb学习总结(二)——Tomcat服务器学习和使用(一) 一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件. ...

  5. 关于Kinect音频开发的探究

    1.笔者在<Kinect体感程序设计入门>(王森著)的这本书中看到可以使用powershell和COM对象无缝整合,轻松的使用windows系统自带的语音合成功能. 步骤:•打开进入pow ...

  6. ActiveMQ部署和503的错误

    最近部署ActiveMQ的时候,发现有的服务器可以打开后台管理网址,有的服务器无法打开,Jetty报503 Service Unavailable. 搞了很久终于发现了问题,现将部署和解决过程做笔记如 ...

  7. Java properties配置文件工具类

    /* * Copyright (c) 2017. Panteng.Co.Ltd All rights reserved */ import org.apache.log4j.Logger; impor ...

  8. java arrays类学习

    java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的. 具有以下功能: (1)给数组赋值:通过fill方法. (2)对数组排序:通过sort方法,按升序. (3)比较数组:通 ...

  9. json对象与字符串互转方法

    字符串转json对象: var data = eval( '(' + str + ')' ); json对象转字符串: var jsonStr = JSON.stringify( obj );

  10. 安装Linux应该知道的十件事

    硬件,软件及其之间的一切 Linux机器的硬件和软件之间一切都由内核主宰. 内核负责硬件和软件的连接,每隔几个礼拜就可以在Internet上获取 更新的内核: 如果你自己安装内核并非易事,这时,就轮到 ...