E - Catch That Cow
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
Output
Sample Input
5 17
Sample Output
4
Hint
#include <cstdio>
#include <queue>
#define MAX 100001
using namespace std;
int n, m;
int ret[MAX], vis[MAX] = {0};
queue<int> q;
int bfs(int n, int m)
{
if(n == m)
return 0;
int cur;
q.push(n);
while(!q.empty())
{
cur = q.front();
q.pop();
if(cur + 1 < MAX && !vis[cur+1])
{
ret[cur+1] = ret[cur] + 1;
vis[cur+1] = 1;
q.push(cur+1);
}
if(cur + 1 == m)
break;
if(cur - 1 >= 0 && !vis[cur-1])
{
ret[cur-1] = ret[cur]+1;
vis[cur-1] = 1;
q.push(cur-1);
}
if(cur - 1 == m)
break;
if(cur * 2 < MAX && !vis[cur*2])
{
ret[cur*2] = ret[cur] + 1;
vis[cur*2] = 1;
q.push(cur*2);
}
if(cur * 2 == m)
break;
}
return ret[m];
}
int main()
{
scanf("%d%d", &n, &m);
printf("%d\n", bfs(n, m));
return 0;
}
E - Catch That Cow的更多相关文章
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj3278 Catch That Cow
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 73973 Accepted: 23308 ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- poj 3278:Catch That Cow(简单一维广搜)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 45648 Accepted: 14310 ...
- 2016HUAS暑假集训训练题 B - Catch That Cow
B - Catch That Cow Description Farmer John has been informed of the location of a fugitive cow and w ...
- HDU 2717 Catch That Cow (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2717 Catch That Cow Time Limit: 5000/2000 MS (Java/Ot ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- Catch That Cow 分类: POJ 2015-06-29 19:06 10人阅读 评论(0) 收藏
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 58072 Accepted: 18061 ...
- [HDOJ2717]Catch That Cow
Catch That Cow Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 2717 Catch That Cow --- BFS
HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...
随机推荐
- SQL语句的MINUS,INTERSECT和UNION ALL
SQL语句中的三个关键字:MINUS(减去),INTERSECT(交集)和UNION ALL(并集); 关于集合的概念,中学都应该学过,就不多说了.这三个关键字主要是对数据库的查询结果进行操作,正如其 ...
- Thread-safety with regular expressions in Java
As mentioned in our introduction to the Pattern and Matcher classes, the Java regular expression API ...
- 多线程编程之二 ---MFC中的多线程开发
下载源代码 五.MFC对多线程编程的支持 MFC中有两类线程,分别称之为工作者线程和用户界面线程.二者的主要区别在于工作者线程没有消息循环,而用户界面线程有自己的消息队列和消息循环. 工作者线程没有消 ...
- InitParam与ContextParm的异同
web.xml里面可以定义两种参数:(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下: xml 代码 <context-param> ...
- YY前端笔试总结
1.一个元素float以后.为什么要清除浮动?清除浮动的方法有哪些? 浮动确实是经经常使用,也知道清除浮动的必要性.但要我道个所以然,还是得绞尽脑汁.我个人的理解是,当一个元素float以后,就脱离正 ...
- MySQL具体解释(19)----------海量数据分页查询优化
分页的详细解说解说请看http://blog.csdn.net/u011225629/article/details/46775947 查看代码打印1 SELECT * FROM table ORDE ...
- 【微信公众号】WeixinJSBridge.call('closeWindow')无效
公众号上面使用iframe嵌套子页面,然而子页面无法使用WeixinJSBridge.call('closeWindow') 这时候必须在函数前面加上parent.,比如parent.WeixinJS ...
- php中0,空,null和false的区别
<? $str1 = null; $str2 = false; echo $str1==$str2 ? ‘相等’ : ‘不相等’; $str3 = ""; $str4 = 0 ...
- ThinkPHP第十八天(Widget类的使用,连贯操作where IN用法,缓存S函数使用)
1.Widget类的使用方法: 第一步:在Action同级目录中新建Widget文件夹(独立分组需要自己建立) 第二步:根据不同功能在Widget文件夹中建立不同的Widget类,如热门文章HotWi ...
- 哈希长度扩展攻击的简介以及HashPump安装使用方法
哈希长度扩展攻击(hash length extension attacks)是指针对某些允许包含额外信息的加密散列函数的攻击手段.该攻击适用于在消息与密钥的长度已知的情形下,所有采取了 H(密钥 ∥ ...