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. PAT (Advanced Level) 1037. Magic Coupon (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  2. PAT (Advanced Level) 1033. To Fill or Not to Fill (25)

    贪心.注意x=0处没有加油站的情况. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  3. sqlite中常见的问题总结

    一.sqlite中不能使用日期进行相减,执行结果无效 例如:SELECT count(*) as cnt FROM DayBanalces WHERE (date(ofDay)- date('2013 ...

  4. webpack-Modules(模块)

    模块(Modules) 在模块化编程中,开发者将程序分解成离散功能块(discrete chunks of functionality),并称之为模块. 每个模块具有比完整程序更小的接触面,使得校验. ...

  5. 避免使用vector&lt;bool&gt;

     作为一个STL容器,vector<bool>仅仅有两点不正确. 首先.它不是一个STL容器. 其次,它并不存储bool.除此之外.一切正常. 一个对象要成为容器,就必须满足C++标准 ...

  6. 用Visual Studio高版本号打开低版本号的project,转换时出现错误:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    解决方法是: 在电脑里面搜索发现   C:\Program Files\Microsoft Visual Studio 10.0\VC\bin   C:\Windows\winsxs\x86_netf ...

  7. 成员函数指针 C++ FAQ LITE — Frequently Asked Questions

    http://www.sunistudio.com/cppfaq/pointers-to-members.html C++ FAQ LITE — Frequently Asked Questions ...

  8. UTF-8 GBK UTF8 GB2312之间的区别和关系

    UTF-8 GBK UTF8 GB2312之间的区别和关系     UTF-8:Unicode TransformationFormat-8bit,允许含BOM,但通常不含BOM.是用以解决国际上字符 ...

  9. linux-shell脚本命令之awk

    [ awk简单介绍: ] awk能够从一个文本中获取部分内容, 或者对这个文本进行排版, 使它按某种格式输出. [ awk工作流程: ] awk会把文件一行内容去到内存里, 然后对这行内容进行分段 ( ...

  10. Win32 Windows编程 七

    定时器消息 1. WM_TIMER 依照定时器设置的时间段,自己主动向窗体发送一个定时器消息WM_TIMER.优先级比較低 定时器精度比較低.毫秒级别.消息产生时间也精度比較低 2 .消息和函数 WM ...