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. 使用windows crypt API解析X509证书
  2. Canvas是什么
  3. Integer Inquiry【大数的加法举例】
  4. wpf 背景镂空loading.....
  5. GitHub详细教程(转载)
  6. Java:过去、未来的互联网编程之王
  7. ☀【canvas】直线 / 三角形 / 矩形 / 曲线 / 控制点 / 变换
  8. Android网络框架---OkHttp3
  9. Python学习之--异常处理
  10. Redis应用场景-整理
  11. 谈谈JavaScript代码混淆
  12. locust 参数,数据详解
  13. BZOJ_1877_[SDOI2009]晨跑_费用流
  14. 研究windows下SVN备份及还原恢复方案
  15. LeetCode 657. Robot Return to Origin
  16. Linux块设备IO子系统(一) _驱动模型
  17. C#连接数据库MD5数据库加密
  18. Scala:Method 小技巧,忽略result type之后的等号
  19. 廖雪峰Java2面向对象编程-6Java核心类-6常用工具类
  20. Android 7.0 Dialog 无法显示的问题

热门文章

  1. HW3.11
  2. Tutorial: Getting Started with SignalR (C#) -摘自网络
  3. 运用Date日期来做日历
  4. 跑马灯效果的TextView之singLine 和maxLines
  5. [SCOI2005]互不侵犯King
  6. Sqlite在Windows、Linux 和 Mac OS X 上的安装过程
  7. MySQL 统计信息
  8. eclipse项目出现红色叉叉解决方案
  9. [Javascript] Object.freeze() vs Object.seal()
  10. 读完了csapp(中文名:深入理解计算机系统)