Catch That Cow

Description

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 - 1 or + 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?

Input

Line 1: Two space-separated integers: N and K

Output

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

Source

USACO 2007 Open Silver

 //2017-02-22
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue> using namespace std; bool book[];
struct node
{
int pos, step;
}; int main()
{
int n, k;
while(cin>>n>>k)
{
memset(book, , sizeof(book));
queue<node> q;
book[n] = ;
node tmp;
tmp.pos = n;
tmp.step = ;
q.push(tmp);
int pos, step;
while(!q.empty())
{
pos = q.front().pos;
step = q.front().step;
q.pop();
if(pos == k){
cout<<step<<endl;
break;
}
if(pos-==k || pos+==k || *pos==k)
{
cout<<step+<<endl;
break;
}
if(pos >= && !book[pos-]){
tmp.pos = pos-;
tmp.step = step+;
q.push(tmp);
book[pos-] = ;
}
if(!book[pos+]){
book[pos+] = ;
tmp.pos = pos+;
tmp.step = step+;
q.push(tmp);
}
if(*pos<=&&!book[pos*]){
book[*pos] = ;
tmp.pos = *pos;
tmp.step = step+;
q.push(tmp);
}
}
} return ;
}

POJ3278(KB1-C 简单搜索)的更多相关文章

  1. ElasticSearch 5学习(4)——简单搜索笔记

    空搜索: GET /_search hits: total 总数 hits 前10条数据 hits 数组中的每个结果都包含_index._type和文档的_id字段,被加入到_source字段中这意味 ...

  2. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  3. 分布式搜索ElasticSearch构建集群与简单搜索实例应用

    分布式搜索ElasticSearch构建集群与简单搜索实例应用 关于ElasticSearch不介绍了,直接说应用. 分布式ElasticSearch集群构建的方法. 1.通过在程序中创建一个嵌入es ...

  4. solr简单搜索案例

    solr简单搜索案例 使用Solr实现电商网站中商品信息搜索功能,可以根据关键字搜索商品信息,根据商品分类.价格过滤搜索结果,也可以根据价格进行排序,实现分页. 架构分为: 1. solr服务器 2. ...

  5. 和我一起打造个简单搜索之SpringDataElasticSearch入门

    网上大多通过 java 操作 es 使用的都是 TransportClient,而介绍使用 SpringDataElasticSearch 的文章相对比较少,笔者也是摸索了许久,接下来本文介绍 Spr ...

  6. 和我一起打造个简单搜索之SpringDataElasticSearch关键词高亮

    前面几篇文章详细讲解了 ElasticSearch 的搭建以及使用 SpringDataElasticSearch 来完成搜索查询,但是搜索一般都会有搜索关键字高亮的功能,今天我们把它给加上. 系列文 ...

  7. 和我一起打造个简单搜索之Logstash实时同步建立索引

    用过 Solr 的朋友都知道,Solr 可以直接在配置文件中配置数据库连接从而完成索引的同步创建,但是 ElasticSearch 本身并不具备这样的功能,那如何建立索引呢?方法其实很多,可以使用 J ...

  8. 和我一起打造个简单搜索之IK分词以及拼音分词

    elasticsearch 官方默认的分词插件,对中文分词效果不理想,它是把中文词语分成了一个一个的汉字.所以我们引入 es 插件 es-ik.同时为了提升用户体验,引入 es-pinyin 插件.本 ...

  9. 和我一起打造个简单搜索之ElasticSearch集群搭建

    我们所常见的电商搜索如京东,搜索页面都会提供各种各样的筛选条件,比如品牌.尺寸.适用季节.价格区间等,同时提供排序,比如价格排序,信誉排序,销量排序等,方便了用户去找到自己心里理想的商品. 站内搜索对 ...

  10. 和我一起打造个简单搜索之ElasticSearch入门

    本文简单介绍了使用 Rest 接口,对 es 进行操作,更深入的学习,可以参考文末部分. 环境 本文以及后续 es 系列文章都基于 5.5.3 这个版本的 elasticsearch ,这个版本比较稳 ...

随机推荐

  1. jQuery基础笔记 each和data(7)

    day56 参考:https://www.cnblogs.com/liwenzhou/p/8178806.html#autoid-1-11-0 each jQuery.each(collection, ...

  2. Linux之开源软件移植

    移植环境 Utuntu 15.04 1.mplayer移植 版本:mplayer-export-snapshot.tar.bz2 /mplayer-export-2015-11-26 Linux PC ...

  3. C#里面BLL、Model、DAL、UI层

    C# 三层架构分为:表现层(UI).业务逻辑层(BLL).数据访问层(DAL)再加上实体类库(Model) 1.实体类库(Model),主要存放数据库中的表字段. 操作: (1)先建立实体类库Mode ...

  4. 深度学习笔记(六)VGG14

    Very Deep Convolutional Networks for Large-Scale Image Recognition 1. 主要贡献 本文探究了参数总数基本不变的情况下,CNN随着层数 ...

  5. chrome inspect 远程调试H5

    chrome://inspect/#devices 一个内置于chrome的远程调试指令,满足远程调试的几个必须条件 1,能够访问https://chrome-devtools-frontend.ap ...

  6. java --Method

    MethodDemo class MethodDemo { public static void main(String[] args) { //调用自定义的方法. draw(4,5); draw(8 ...

  7. manjaro开启sdd trim

      $ sudo systemctl enable fstrim.timer; $ sudo systemctl start fstrim.service; $ sudo systemctl stat ...

  8. freeSWITCH之安装

    freeSWITCH 安装 官网教程 https://freeswitch.org/confluence/display/FREESWITCH/FreeSWITCH+First+Steps Windo ...

  9. <转载> 58到家数据库设计规范

    原文地址: http://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651959906&idx=1&sn=2cbdc66cf ...

  10. 蝴蝶效应--由'sudo -s ...'引发的vim autocmd使用异常

    1. 背景介绍 自加入RedHat起,我就把家里的台式机(Ubuntu 16.04 LTS)的默认登录用户veli切换成了huanli, 主要是为了跟公司的电脑配置对齐以方便未来WFH,但引发了一个v ...