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. Codeforces 658C Bear and Forgotten Tree 3【构造】

    题目链接: http://codeforces.com/contest/658/problem/C 题意: 给定结点数,树的直径(两点的最长距离),树的高度(1号结点距离其他结点的最长距离),写出树边 ...

  2. 继续畅通工程--hdu1879(最小生成树 模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1879 刚开始么看清题  以为就是n行  后来一看是n*(n-1)/2行   是输入错误  真是够够的 #incl ...

  3. POJ 3281 [网络流dinic算法模板]

    题意: 农场主有f种食物,d种饮料,n头牛. 接下来的n行每行第一个数代表第i头牛喜欢吃的食物数量,和第i头牛喜欢喝的饮料数目. 接下来分别是喜欢的食物和饮料的编号. 求解:农场主最多能保证几头牛同时 ...

  4. java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

    新建Maven 项目的时候报错: java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet ...

  5. http://www.16aspx.com/Code/Show/5352

    http://www.16aspx.com/Code/Show/5352 可视化工作流引擎RoadFlowV1.3 http://www.cnblogs.com/f2flow/p/4212678.ht ...

  6. Effective Java Profiling With Open Source Tools

    https://www.infoq.com/articles/java-profiling-with-open-source

  7. how to read openstack code: request extension

    We have learned resource extension and action extension. This post we will write a request extension ...

  8. gbk转utf-8 iconv 编码转换

    linux以下有时候 字符须要进行编码转换(爬虫将gbk转为utf-8编码...).一般能够选择iconv函数. 终端以下  输入 man 3 iconv 得到  iconv函数的用法. 个人看习惯了 ...

  9. Git撤销&amp;回滚操作

    开发过程中.你肯定会遇到这种场景: 场景一: 糟了.我刚把不想要的代码.commit到本地仓库中了.可是还没有做push操作! 场景二: 彻底完了.刚线上更新的代码出现故障了.须要还原这次提交的代码! ...

  10. 网络知识: 物理层PHY 和 网络层MAC

    PHY模块简介 物理层位于OSI最底层,物理层协议定义电气信号.线的状态.时钟要求.数据编码和数据传输用的连接器. 物理层的器件称为PHY. 上图里的灰色方框图里的就是PHY芯片内部模块图. MAC器 ...