【OpenJ_Bailian - 4001】 Catch That Cow(bfs+优先队列)
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 移动到点 2 × X,也只需要一分钟。
如果那头逃走的牛并不知道对它实施的抓捕行动,因此完全不移动,那么农夫约翰花多少时间可以抓住这头牛?
Input
第一行包含两个以空格分隔的整数: N 和 K
Output
第一行输出农夫约翰抓住逃走的牛,所花费的最短时间 (分钟)。
Hint
农夫约翰抓住逃走的牛的最快方式,是按如下路径移动:5-10-9-18-17,这花费了 4 分钟。
#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+优先队列)的更多相关文章
- 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,最先 ...
- POJ3278——Catch That Cow(BFS)
Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- 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 ...
- POJ3278 Catch That Cow —— BFS
题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total S ...
- catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 38263 Accepted: 11891 ...
- POJ3278 Catch That Cow(BFS)
Description Farmer John has been informed of the location of a fugitive cow and wants to catch her i ...
随机推荐
- hdu——3861 The King’s Problem
The King’s Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- [Bzoj4182]Shopping(点分治)(树上背包)(单调队列优化多重背包)
4182: Shopping Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 374 Solved: 130[Submit][Status][Disc ...
- Java模拟斗地主(实现大小排序)
import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.Li ...
- 基于unicorn-engine的虚拟机的实现(WxSpectre)
反病毒虚拟机是一个很有优势的工具,可以说反病毒软件是否存在模拟器是衡量反病毒软件能力的一个指标.反病毒虚拟机不光是内嵌在反病毒软件内部,来动态执行样本.这种虚拟机一般也可以单独用来动态执行批量样本,检 ...
- md5sum使用注意事项
1. linux 命令行的 md5sum命令 echo -n "123456" | md5sum //echo的时候, 默认是自带回车的, 必须要去掉 -n 去掉回车. 2 ...
- [Javascript] Link to Other Objects through the JavaScript Prototype Chain
Objects have the ability to use data and methods that other objects contain, as long as it lives on ...
- activiti自己定义流程之自己定义表单(二):创建表单
注:环境配置:activiti自己定义流程之自己定义表单(一):环境配置 在上一节自己定义表单环境搭建好以后,我就正式開始尝试自己创建表单,在后台的处理就比較常规,主要是针对ueditor插件的功能在 ...
- C#结构类型图
C#结构类型图 分类: C#
- wsgiref — WSGI Utilities and Reference Implementation nginx
from wsgiref.util import setup_testing_defaults, request_urifrom wsgiref.simple_server import make_s ...
- HttpServlet容器响应Web客户流程
HttpServlet容器响应Web客户请求流程如下: 1)Web客户向Servlet容器发出Http请求: 2)Servlet容器解析Web客户的Http请求: 3)Servlet容器创建一个Htt ...