Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 36079   Accepted: 11123

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.

【题目来源】

【题目大意】
在x轴的正半轴上,一个人的出发点是N,一头牛在另一点K,人可以有两种操作:
1.步行:+1 or -1
2.传送:*2
问你通过最少的步数到达牛的位置,需要多少步。
 
【题目分析】
就是一个裸的广搜,使用队列实现。
 
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int N,K; struct now
{
int x;
int step;
};
bool vis[]; int BFS(int x)
{
int tx;
now ans,tp;
queue<now> que;
ans.x=x,ans.step=;
vis[x]=true;
que.push(ans);
while(!que.empty())
{
tp=que.front();
que.pop();
tx=tp.x+; //no.1
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x-; //no.2
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
tx=tp.x*; //no.3
if(tx<||tx>);
else
{
if(!vis[tx])
{
vis[tx]=true;
ans.x=tx,ans.step=tp.step+,que.push(ans);
if(ans.x==K) return ans.step;
}
}
}
} int main()
{
while(cin>>N>>K)
{
if(N==K)
{
cout<<""<<endl;
continue;
}
memset(vis,false,sizeof(vis));
cout<<BFS(N)<<endl;
}
return ;
}

当然还可以剪枝一下,比如说:如果人的坐标大于牛的坐标,这时就只需要做-1这一步就可以了。

最新文章

  1. android 开发中 添加库文件 和so 文件的存放位置和添加依赖
  2. 三言两语聊Python模块–文档测试模块doctest
  3. ElasticSearch 2 (6) - 插件安装Head、Kopf与Bigdesk
  4. mac mysql
  5. 常用vs快捷键
  6. date命令--修改linux系统时间
  7. 《chkconfig命令》-linux命令五分钟系列之四
  8. [BZOJ 1084] [SCOI2005] 最大子矩阵 【DP】
  9. Android 手机上安装并运行 Ubuntu 12.04(转,没实测)
  10. 如何自定义JSR-303标准的validator
  11. 五年级--python函数高级运用
  12. 将Python脚本打包成可执行文件
  13. 转载:【Scala】使用Option、Some、None,避免使用null
  14. CSS之form&amp;span
  15. selective search
  16. Ubuntu 安装 VS code
  17. Android 跳转到系统应用管理
  18. 25个iptables常用示例
  19. vue 脚手架(二,项目依赖说明 package.json)
  20. .Net生成导出Excel

热门文章

  1. 【夯实基础】-浅谈&quot;单点登录&quot;的几种实现方式
  2. 前端小插件之手写js循环滚动特效
  3. PHP生成唯一用户标识GUID
  4. find、locate、whereis、which和type
  5. nginx之旅(第三篇):代理、正向代理、反向代理、代理的原理、nginx反向代理场景、nginx反向代理配置、nginx反向代理语法
  6. angular6 监听url查询参数变化刷新页面
  7. spring项目使用mave将第三方jar包打进war包中
  8. clickjacking 攻击
  9. 不要让事实妨碍好故事:Facebook精准广告产品与硅谷创业揭秘,4星奇书《混乱的猴子》
  10. PAT 乙级 1047.编程团体赛 C++/Java