Data Structure - Week 14 (B)
2012-12-30 00:00
706 查看
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
牛不动,Farmer John每一步有两种选择,要求最少耗时,故采用广度优先搜索算法。
* 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
牛不动,Farmer John每一步有两种选择,要求最少耗时,故采用广度优先搜索算法。
相关文章推荐
- Data Structure - Week 14 (A)
- 团队作业Week14——源代码管理
- week14---12月11日 JS内置对象——document文档对象、Dom对象
- <<Oracle Applications DBA 基础(第二期)>>Week 14 exercise
- LeetCode week 14: Coin Change
- LeetCode#494 Target Sum (week14)
- week14---12月8日 JS内置对象——string、Date
- 【算法设计作业】week14
- Strange Printer_Week14
- week_14_ Word Search
- Live NFL playoff probability updates for Week 14
- 小课堂week14 Google软件测试之道
- week14---补充泛型
- week14---12月9日 JS内置对象——Math、Boolean、Number对象
- [leetcode contest 582. Kill Process]week 14
- LeetCode#648 Replace Words (week14)
- Guru of the Week 条款14:类之间的关系(上篇)
- week 14
- week14_2.cpp
- Leetcode: 216. Combination Sum III(Week14, Medium)