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. django 获取外键对应数据的方式

    模型 models.py中 from django.db import models class User(models.Model): name = models.CharField() class ...

  2. python 爬爬爬 基本函数~

    https://docs.python.org/2/howto/urllib2.html#data http://zhuoqiang.me/python-urllib2-usage.html #cod ...

  3. sublime text 给选中项插入编号

    #coding=utf-8 import datetime, getpass import sublime, sublime_plugin import re # 插数字 class InsertNu ...

  4. springmvc执行流程详细介绍

    1.什么是MVC MVC是Model View Controller的缩写,它是一个设计模式 2.springmvc执行流程详细介绍 第一步:发起请求到前端控制器(DispatcherServlet) ...

  5. easyUI combobox下拉框很长,easyUI combobox下拉框如何显示滚动条的解决方法

    如下图,combobox下拉框里内容很多时,会导致下拉框很长,很不美观. 如何使得combobox下拉框显示滚动条 方法:把属性panelHeight:"auto"注释掉即可. $ ...

  6. Spark之Scala学习

    1. Scala集合学习: http://blog.csdn.net/lyrebing/article/details/20362227 2. scala实现kmeans算法 http://www.t ...

  7. Storm累计求和Demo并且在集群上运行

    打成jar包放在主节点上去运行. import java.util.Map; import backtype.storm.Config; import backtype.storm.StormSubm ...

  8. (转)Python rsplit() 方法

    原文:https://www.cnblogs.com/wushuaishuai/p/7792874.html 目录 描述 语法 参数 返回值 实例 正文 回到顶部 描述 Python rsplit() ...

  9. SpringMVC3.2+Spring3.2+Mybatis3.1(SSM~Demo)

    SpringMVC+Spring+Mybatis 框架搭建 整个Demo的视图结构: JAR: 下载地址:http://download.csdn.net/detail/li1669852599/85 ...

  10. 解决ajax跨域问题的一种方法

    解决ajax跨域问题的一种方法 前后端分离经常用json来传输数据,比较常见的问题就有ajax跨域请求的错误问题,这里是我的一种解决方法: 在java中加入如下的注解类: import org.spr ...