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

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

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 logging模块

    1.简单的将日志打印到屏幕   import logging logging.debug('This is debug message')logging.info('This is info mess ...

  2. Loadrunder之脚本篇——检查点

    VuGen判断脚本是否执行成功是根据服务器返回的状态来确定的,如果服务器返回的是HTTP状态为200 OK,那么VuGen就认为脚本正确地运行了,并且是运行通过的.而大多数系统出错时是不会返回错误页面 ...

  3. mongodb简单用法

    修改器: $inc: 增加已有的键值,如果键值不存在就创建一个 数据库中存在这样的数据:{ , "url": "www.example.com", }db.fz ...

  4. SpringBoot2.1.0 application.properties配置

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  5. nf_conntrack之解决方案

    1.现象 在/var/log/message中出现以下信息 Dec 8 11:22:29 product08 kernel: nf_conntrack: table full, dropping pa ...

  6. jQuery计算器插件

    在线演示 本地下载

  7. 什么是make config,make menuconfig,make oldconfig,make xconfig,make defconfig,make gconfig?【转】

    本文转载自;https://blog.csdn.net/baweiyaoji/article/details/52876701 在进行内核配置,或者是对一些软件的配置和编译中,常常会遇到: make ...

  8. Jquery 获取地址位置

    直接在浏览器地址 输入: http://pv.sohu.com/cityjson?ie=utf-8 可以查看数据格式 引入一个搜狐的js库: <script src="http://p ...

  9. 解决Linux系统在设置alias命令重启后失效的问题

    在使用linux系统的过程中,大多数情况下都是在字符界面下进行的.有些比较长的命令我们不希望每次都重复输入,这样不仅浪费时间而且还容易出错:我们会使用alias命令来解决 比如: alias ll=' ...

  10. Nginx location指令匹配顺序规则

    location匹配命令 1. “= ”,字面精确匹配, 如果匹配,则跳出匹配过程.(不再进行正则匹配) 2. “^~ ”,最大前缀匹配,如果匹配,则跳出匹配过程.(不再进行正则匹配) 3. 不带任何 ...