OpenJ_Bailian - 4001 Catch That Cow(简单bfs+优先队列)

农夫约翰寻找逃走的牛,从点N出发,通过行走和神行两种方式尝试抓住在点K的牛。本题通过广度优先搜索(BFS)结合优先队列,求解找到牛的最短时间。当牛不移动时,农夫约翰在4分钟后到达牛的位置(5-10-9-18-17)。
摘要由CSDN通过智能技术生成

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的判断就是,农夫现在的地方等于牛的地方。

上代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值