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 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?

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.
 
 
问题分析:
简单bfs可以解答,分支为step+1;step-1;以及step*2. 第一次使用bfs以及容器;据说用容器比较慢,有时间想一想替代的方法0.0
 
 #include "iostream"
#include "queue" using namespace std;
char a[];
void mset()
{
for (int i=;i<;i++)
a[i] = '';
}
struct far
{
int w;
int step;
};
int bfs(int n,int k)
{
if (n>=k)
return n-k;
queue<far> q;
far fir,sec;
fir.w = n;
fir.step =;
a[n] = '';
q.push(fir);
while (!(q.empty()))
{
sec=q.front();
q.pop();
for (int i=;i<=;i++)
{
switch (i)
{
case : fir.w=sec.w+; break;
case : fir.w=sec.w-; break;
case : fir.w=sec.w*; break;
}
fir.step=sec.step+;
if (fir.w == k) return fir.step;
if (fir.w>= && fir.w<= && a[fir.w] == '' )
{
a[fir.w] ='';
q.push(fir);
}
}
}
}
int main()
{
int n,k;
while (cin>>n>>k)
{
mset();
cout<<bfs(n,k)<<endl;
}
return ;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

暑假集训(1)第二弹 -----Catch the cow(Poj3278)的更多相关文章

  1. 2016HUAS暑假集训训练题 B - Catch That Cow

    B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...

  2. 暑假集训(4)第二弹 -----递推(hdu2254)

    题意概括:上次小A在你的帮助下成功炼成贤者法阵的第一部分——三角分隔,现在他准备绘制法阵的第二部分——莫测矩形. 而他又遇到了一个问题,他不知道不同矩形到底有多少个. 秉持帮人帮到底,送佛送到西的基本 ...

  3. 暑假集训(3)第二弹 -----Jungle Roads(Hdu1301)

    问题梗概:自从上次某个acmer来设计了拉格瑞圣岛的交通路线后,岛上的酋长就相当苦恼,他发现,虽然这些修好的公路便利了岛上的 交通,并且让拉格瑞圣岛的旅游业更加兴旺,甚至他们还收到了一笔不小的国际资金 ...

  4. 暑假集训(5)第二弹———湫湫系列故事——减肥记I(hdu4508)

    问题描述:舔了舔嘴上的油渍,你陷在身后柔软的靠椅上.在德源大赛中获得优胜的你,迫不及待地赶到“吃到饱”饭店吃到饱.当你 正准备离开时,服务员叫住了你,“先生,您还没有吃完你所点的酒菜.”指着你桌上的一 ...

  5. 暑假集训(2)第二弹 ----- The Suspects(POJ1611)

    B - The Suspects Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:20000KB   ...

  6. 抓住那只牛!Catch That Cow POJ-3278 BFS

    题目链接:Catch That Cow 题目大意 FJ丢了一头牛,FJ在数轴上位置为n的点,牛在数轴上位置为k的点.FJ一分钟能进行以下三种操作:前进一个单位,后退一个单位,或者传送到坐标为当前位置两 ...

  7. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

  8. 暑假集训(4)第一弹 -----递推(Hdu2039)

    题意梗概:fff团团员小A想退团了,不过要退团,他必须绘制出贤者法阵,以证明他有资格不受大fff之灵的监督 并退团,小A他现在要开始收集材料了,但是,他不清楚应该买多少份材料. 虽然你并不想帮他退团, ...

  9. 暑假集训(5)第一弹——— Super Jumping! Jumping! Jumping!(hdu1087)

    题意概括:在上次与娑殚的三次博弈中,你们都取得了胜利.便向娑殚提出要求,借助他的力量,传送到一个安全的地方. 你们的愿望达成了,不过,你和小A似乎失散了. 街上人来人往的特别热闹,每一个人的脸上都洋溢 ...

随机推荐

  1. HDOJ-ACM1004(JAVA)

    转载声明:原文转自http://www.cnblogs.com/xiezie/p/5502876.html JAVA语言实现: 拿到题目第一反应是用HashMap实现,但是感觉用HashMap实在太浪 ...

  2. 潜语义分析(Latent Semantic Analysis)

    LSI(Latent semantic indexing, 潜语义索引)和LSA(Latent semantic analysis,潜语义分析)这两个名字其实是一回事.我们这里称为LSA. LSA源自 ...

  3. tool

    数据结构,堆栈基本原理; 数组,堆栈,结构体等系列,数据结构间区别联系; 在此我就不饶了,直接上大白话(我总认为干些实事,比扯淡强....),故事虚构,若有雷同,请你下方留言; ruiy讲的,所跑的托 ...

  4. PHP函数补完:var_export()

    var_export() 函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码.var_export必须返回合法的php代码, 也就是 ...

  5. sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0

    sqlplus 连接数据库报错SP2-0642: SQL*Plus internal error state 2130, context 0:0:0 问题描述: 使用sqlplus客户端登录数据库,报 ...

  6. Android框架结构图

  7. 在DDMS中访问data目录

    转自 http://blog.csdn.net/catoop/article/details/7584746 由于某些原因,需要通过eclipse访问/data/下的文件. 打开eclipse-DDM ...

  8. Fatal error: Call to undefined function mysql_connect()

    我在进行PHP环境搭建:Windows 7下安装配置PHP+Mysql+apache环境时,之前都没有什么问题,只是在验证PHP是否能连接Mysql时出现如下错误:Fatal error: Call ...

  9. JavaScript实现竖直文本滚动

    一.HTML代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  10. cocos2dx lua学习笔记 &lt;一&gt; quick 3.5定义本身C++类是必然lua

    请尊重原创 转载有名源:http://blog.csdn.net/wushao126/article/details/46660375 首先去官网下载最新的quick.配置好环境.创建一个luapro ...