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这一步就可以了。

BFS --- 模板题的更多相关文章

  1. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  2. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  3. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  4. Saving Princess claire_(hdu 4308 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...

  5. Knight Moves(hdu1372 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)   ...

  6. 用一道模板题理解多源广度优先搜索(bfs)

    题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...

  7. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  8. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  9. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. Mybatis逆向工程的使用。

    指定配置文件与main运行生成 public class GeneratorSqlmap { public void generator() throws Exception { List<St ...

  2. Gin-Go学习笔记二:Gin-Web框架

    Gin-Web框架 1>     首先声明,这个是我自己搭建的纯Gin-Web框架,其中有借鉴学习别的想法和代码.已上传到GitHub上.地址为: https://github.com/weiy ...

  3. js正则只能包含小写数字分割符,切不能以分割符开头和结尾

    const version = /^(?!_)(?!.*-$)[a-z0-9_]+$/; 1.一个正则表达式,只含有数字.小写字母.中划线不能以中划线开头和结尾: ^(?!-)(?!.*-$)[a-z ...

  4. vue通过Blob实现下载文件

    需求是这样的...... 具体实现,前端拿到后端返回回来的数据,然后通过Blob实现下载,文件内容样式啥的都是后端写的 script代码: 这里的data就是后端返回回来的数据,此方法兼容IE dow ...

  5. cpio建立、还原备份档

    1. 简介 加入.解开cpio或tar备份档内的文件 与tar相似,将文件归档到硬盘或磁带等存储设备中 2. tar比较 在所处理的文件类型方面,它比tar更全面,但也更复杂 cpio比tar更为可靠 ...

  6. 使用Fiddler监听java HttpURLConnection请求

    使用Fiddler监听java HttpURLConnection请求

  7. eclipse 搭建springboot项目pom.xml报错

    1. 报错信息 2. 解决方法 在pom.xml文件中加入maven版本修改 <maven-jar-plugin.version>3.1.1</maven-jar-plugin.ve ...

  8. 给大家推荐一个nginx.conf文件格式生成的网站

    特别好用: 根据选择自定义nginx.conf的配置: https://nginxconfig.io/?0.php=false&0.python&php_server=%2Fvar%2 ...

  9. 词向量---ELMO

    1.ELMo(Embeddings from Language Models ) RNN-based language models(trained from lots of sentences) E ...

  10. 关系型数据库 RDS(Relational Database Service),知识点

    资料 网址 官方介绍 https://help.aliyun.com/document_detail/26092.html?spm=5176.2020520104.0.0.2b4b1450yqd1gg ...