Catch That Cow

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7147    Accepted Submission(s): 2254
Problem 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.
 

#include <stdio.h>
#include <queue>
#include <string.h>
#define maxn 100002
using std::queue; struct Node{
int pos, step;
};
bool vis[maxn]; void move(Node& tmp, int i)
{
if(i == 0) --tmp.pos;
else if(i == 1) ++tmp.pos;
else tmp.pos <<= 1;
} bool check(int pos)
{
return pos >= 0 && pos < maxn && !vis[pos];
} int BFS(int n, int m)
{
if(n == m) return 0;
memset(vis, 0, sizeof(vis));
queue<Node> Q;
Node now, tmp;
now.pos = n; now.step = 0;
Q.push(now);
vis[n] = 1;
while(!Q.empty()){
now = Q.front(); Q.pop();
for(int i = 0; i < 3; ++i){
tmp = now;
move(tmp, i);
if(check(tmp.pos)){
++tmp.step;
if(tmp.pos == m) return tmp.step;
vis[tmp.pos] = 1;
Q.push(tmp);
}
}
}
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m) == 2){
printf("%d\n", BFS(n, m));
}
return 0;
}

HDU2717 Catch That Cow 【广搜】的更多相关文章

  1. hdu 2717 Catch That Cow(广搜bfs)

    题目链接:http://i.cnblogs.com/EditPosts.aspx?opt=1 Catch That Cow Time Limit: 5000/2000 MS (Java/Others) ...

  2. hdu2717 Catch That Cow

    http://acm.hdu.edu.cn/showproblem.php?pid=2717 //水搜... #include<stdio.h> #include<math.h> ...

  3. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  4. hdu 2717:Catch That Cow(bfs广搜,经典题,一维数组搜索)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. Catch That Cow(BFS广搜)

    Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...

  6. Catch That Cow(广搜)

    个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John h ...

  7. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  8. 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 ...

  9. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

随机推荐

  1. Thinking in Java---异常处理机制

    java的异常处理机制能够使程序有极好的容错性,让程序更加的健壮.所谓的异常,就是指的阻止当前方法或作用域继续运行的问题,,当程序运行时出现异常时,系统就会自己主动生成一个Exception对象来通知 ...

  2. Caused by: java.lang.IllegalArgumentException: Can not set int field reyo.sdk.enity.xxx.xxx to java.lang.Long

    由于数据库字段设置不正确引起的,不能选中 alter <table> modify <column> int unsigned; 关于unsigned int类型,可以看看它的 ...

  3. 能添加图标的label

    能添加图标的label 效果 源码 https://github.com/YouXianMing/UI-Component-Collection 中的 IconEdgeInsetsLabel // / ...

  4. 实用ExtJS教程100例-002:MessageBox的三种用法

    在上一节中,我们用到了MessageBox,在本文中,我们将介绍一下ExtJS中常用的三种MessageBox. Ext.MessageBox.alert() 这个方法用来打开一个普通的对话框,对话框 ...

  5. HTML5 a标签的download属性

    介绍一个HTML5的新特性 a标签的download属性: 目前市场上面支持的浏览器有限: html: <!DOCTYPE html> <html> <body> ...

  6. Reloading Java Classes 301: Classloaders in Web Development — Tomcat, GlassFish, OSGi, Tapestry 5 and so on Translation

    The Original link : http://zeroturnaround.com/rebellabs/rjc301/ Copyright reserved by Rebel Inc In t ...

  7. IE9版本以下ajax 跨域问题解决

    ajax跨域请求数据在谷歌火狐我本地IE11都是没问题的. 让测试就发现问题了,IE8下请求不到数据,然后我查看一下自己写的js看有没有不兼容问题,可是都没有啊,为什么就请求不到呢. 我把ajax的e ...

  8. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  9. Verilog 加法器和减法器(2)

    类似半加器和全加器,也有半减器和全减器. 半减器只考虑当前两位二进制数相减,输出为差以及是否向高位借位,而全减器还要考虑当前位的低位是否曾有借位.它们的真值表如下: 对半减器,diff = x ^y, ...

  10. java问题排查命令

    java问题排查命令 jps:查看java进程 jmap:导出堆详细信息(与jhat一起使用) jhat:分析Java堆的命令(与jmap一起使用) jstack:可以定位到线程堆栈,根据堆栈信息我们 ...