Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 88361   Accepted: 27679

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; int que[];
int start=,endd=;
int time[]={};
int vis[]={};
int n,k; int bfs(int n,int k){
memset(que,,sizeof(que));
memset(time,,sizeof(time));
memset(vis,,sizeof(vis));
start=;
endd=;
que[endd++]=n;
vis[n]=;
while(start<endd){
int t=que[start];
start++;
for(int i=;i<;i++){
int tt=t;
if(i==){
tt+=;
}else if(i==){
tt-=;
}else if(i==){
tt*=;
}
if(tt>||tt<){
continue;
}
if(!vis[tt]){
vis[tt]=;
que[endd]=tt;
time[tt]=time[t]+;
if(tt==k){
return time[tt];
}
endd++;
}
}
}
} int main()
{
int ans;
while(~scanf("%d %d",&n,&k)){
if(n<k){
ans=bfs(n,k);
}else{
ans=n-k;
}
printf("%d\n",ans);
}
return ;
}

poj3278Catch That Cow的更多相关文章

  1. poj3278Catch That Cow(BFS)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37094   Accepted: 11466 ...

  2. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  3. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

  4. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

  5. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  6. [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

    [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...

  7. 细读cow.osg

    细读cow.osg 转自:http://www.cnblogs.com/mumuliang/archive/2010/06/03/1873543.html 对,就是那只著名的奶牛. //Group节点 ...

  8. POJ 3176 Cow Bowling

    Cow Bowling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 8598 Desc ...

  9. raw,cow,qcow,qcow2镜像的比较

    在linux下,虚拟机的选择方式有很多,比如vmware for linux,virtual box,还有qemu,在以前,使用qemu的人不多,主要是使用起来有些麻烦,但现在随着Openstack的 ...

随机推荐

  1. 也谈谈我对Docker的简单理解

    Docker能解决什么问题呢?一个工具的出现必然需要解决一些问题,Docker也不例外,简单说说我们常见的2种情况Docker是如何解决的吧.1.程序在我这跑得好好的,在你那怎么就不行呢?!这是一个典 ...

  2. ASP.NET WebApi 基于OAuth2.0实现Token签名认证

    一.课程介绍 明人不说暗话,跟着阿笨一起玩WebApi!开发提供数据的WebApi服务,最重要的是数据的安全性.那么对于我们来说,如何确保数据的安全将是我们需要思考的问题.为了保护我们的WebApi数 ...

  3. C# Task WhenAny和WhenAll 以及TaskFactory 的ContinueWhenAny和ContinueWhenAll的实现

    个人感觉Task 的WaitAny和WhenAny以及TaskFactory 的ContinueWhenAny有相似的地方,而WaitAll和WhenAll以及TaskFactory 的Continu ...

  4. Get the client's IP address in socket.io

    From: https://www.wentong.org/codex/question-2018081564702.html When using socket.IO in a Node.js se ...

  5. MyBatis架构与源码分析<资料收集>

    1.架构与源码分析 :https://www.cnblogs.com/luoxn28/p/6417892.html .https://www.cnblogs.com/wangdaijun/p/5296 ...

  6. 每天一个linux命令:du

    1.命令简介 du (Disk usage) 用来计算每个文件的磁盘用量,目录则取总用量. 2.用法 用法:du [选项]... [文件]... 或:du [选项]... --files0-from= ...

  7. 同步IO、异步IO、阻塞IO、非阻塞IO之间的联系与区别

    POSIX 同步IO.异步IO.阻塞IO.非阻塞IO,这几个词常见于各种各样的与网络相关的文章之中,往往不同上下文中它们的意思是不一样的,以致于我在很长一段时间对此感到困惑,所以想写一篇文章整理一下. ...

  8. 面试汇总——知道什么是同源策略吗?那怎么解决跨域问题?知道 JSONP 原理吗?

    本文是面试汇总分支——知道什么是同源策略吗?那怎么解决跨域问题?知道 JSONP 原理吗?. 同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能, ...

  9. 分析轮子(六)- LinkedList.java

    注:玩的是JDK1.7版本 一:先上类的继承结构图 二:再看一下他的底层实现数据结构 三:然后从源码中找点好玩的东西 1)双向链表的结构构成元素,头指针.尾指针.节点信息(前向指针.后向指针.节点信息 ...

  10. 使用MiniProfiler调试ASP.NET web api项目性能

    本质上,集成Miniprofiler可以分解为三个问题: 怎样监测一个WebApi项目的性能. 将性能分析监测信息从后端发送到UI. 在UI显示分析监测结果. 首先安装Miniprofiler,Min ...