Catch That Cow


Descriptions:

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.

题目链接:
 
题目大意
农夫约翰获知了一头逃走的牛的位置,想要将它立即抓住。他从数轴上的一个点 N (0 ≤ N ≤ 100,000) 出发,牛位于相同数轴上的点 K (0 ≤ K ≤ 100,000)。农夫约翰有两种前进方式:行走和神行。
行走:农夫约翰可以从任何 X 移动到点 X - 1 或 X + 1,只需要一分钟;
神行:农夫约翰可以从任何点 X 移动到点 2 × X,也只需要一分钟。
如果那头逃走的牛并不知道对它实施的抓捕行动,因此完全不移动,那么农夫约翰花多少时间可以抓住这头牛?
Input
第一行包含两个以空格分隔的整数: N 和 K
Output
第一行输出农夫约翰抓住逃走的牛,所花费的最短时间 (分钟)。
Hint
农夫约翰抓住逃走的牛的最快方式,是按如下路径移动:5-10-9-18-17,这花费了 4 分钟。
 
这题我的想法就是bfs,加个优先队列吧,毕竟求最小话费时间,优先队列是一定没有错的,个人习惯写法。大致判断一下农夫下一步该走到哪里,那个地方是否满足2个条件:是否在0-100000之间;这个点是否走过。符合这两点就能入队了。break的判断就是,农夫现在的地方等于牛的地方,写的比较粗糙,见谅。
 
AC代码
 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
using namespace std;
int N,K;
int vis[];//路径判断是否走过
struct node
{
int n,k;//n为现在的地方,k为牛的地方
int step;//步数即时间
friend bool operator<(node a,node b)//步数小的现出来,即时间少
{
return a.step>b.step;
}
};
priority_queue<node>q;
void bfs()
{
node now;//初始化now
now.n=N;
now.k=K;
now.step=;
q.push(now);
while(!q.empty())
{
node mid=q.top();
q.pop();
if(mid.n==mid.k)//农夫现在的地方等于牛的地方,即找到牛了
{
cout << mid.step <<endl;
return;
}
//农夫的三种走法判断是否符合题意
if(mid.n+>=&&mid.n+<=&&!vis[mid.n+])
{
node last;//更新队列
last.k=K;
last.n=mid.n+;
last.step=mid.step+;//步数+1
vis[mid.n+]=;//标记路径
q.push(last);//入队
}
if(mid.n->=&&mid.n-<=&&!vis[mid.n-])
{
node last;
last.k=K;
last.n=mid.n-;
last.step=mid.step+;
vis[mid.n-]=;
q.push(last);
}
if(mid.n*>=&&mid.n*<=&&!vis[mid.n*])
{
node last;
last.k=K;
last.n=mid.n*;
last.step=mid.step+;
vis[mid.n*]=;
q.push(last);
}
}
}
int main()
{
memset(vis,,sizeof(vis));//初始化路径都为0,即没有走过
cin >> N >> K;
vis[N]=;
bfs();
return ;
}

【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)的更多相关文章

  1. 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,最先 ...

  2. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

  3. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

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

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

  5. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  6. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  7. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  8. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  9. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  10. POJ3278 Catch That Cow(BFS)

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

随机推荐

  1. React学习之State

    本文基于React v16.4.1 初学react,有理解不对的地方,欢迎批评指正^_^ 一.定义组件的两种方式 1.函数定义组件 function Welcome(props) { return & ...

  2. Unique Paths II (dp题)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  3. 【algorithm】尾递归

    尾递归和一般的递归不同在对内存的占用,普通递归创建stack累积而后计算收缩,尾递归只会占用恒量的内存(和迭代一样).SICP中描述了一个内存占用曲线,用以上答案中的Python代码为例(普通递归): ...

  4. requests库帮助

    requests库帮助 http://docs.python-requests.org/zh_CN/latest/user/quickstart.html

  5. POJ3262 Protecting the Flowers 【贪心】

    Protecting the Flowers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4418   Accepted: ...

  6. 性能监控 -- 中间件性能监控【Weblogic控制台】

    通过WebLogic管理控制台可以实时获取各性能指标,通过控制台,可以对weblogic的性能及运行状况,发布的应用.资源等进行监视 1. 进入Weblogic管理控制台,单击服务器,选择一台需监控的 ...

  7. 【HRS项目】Axure兴许问题解决---与SVN结合

    上一篇博客介绍了Axure的团队开发用法,http://blog.csdn.net/u013036274/article/details/50999139,可是再用的时候发现会出现这种问题,例如以下图 ...

  8. 遍历数据库全部表,将是datetime类型的列的值进行更新

    declare @tablename nvarchar(80)   declare @cloumn nvarchar(80)   declare @sql nvarchar(400) declare ...

  9. 5 微信票据 access_token--开发微信的第二道坎儿

    一 access_token基本概念 定义:access_token是公众号的全局唯一接口调用凭据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存. 时效性:access_ ...

  10. 用bis和bic实现位级操作

    20世纪70年代末至80年代末,DigitalEquipment的VAX计算机是一种非常流行的机型.它没有布尔运算AND和OR指令,仅仅有bis(位设置)和bic(位清除)这两种指令.两种指令的输入都 ...