Catch That Cow-抓住那头牛(BFS+队列)

 Catch That Cow 

HDU - 2717 

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.

农夫约翰已被告知一头逃亡母牛的位置,并希望立即抓住她。他从数轴上的点 N (0 ≤ N ≤ 100,000) 开始,而奶牛在同一数轴上的点 K (0 ≤ K ≤ 100,000) 处。Farmer John 有两种交通方式:步行和传送。

* 行走:FJ 可以在一分钟内从 X 点移动到 X - 1 或 X + 1 点
* 传送:FJ 可以在一分钟内 从 X 点移动到 2 × X 点。

如果母牛不知道它的追逐,根本不动,农夫约翰需要多长时间才能取回它?

输入

第 1 行:两个空格分隔的整数:N 和 K

输出

第 1 行:Farmer John 抓住逃亡母牛所需的最少时间(以分钟为单位)。

Sample Input

5 17

Sample Output

4

这道题 和A strange lift(奇怪的电梯)  HDU - 1548 差不多,道理都一样,都是利用队列和bfs                                       

上一篇:UVA 439 Knight Moves 解题报告


下一篇:状态BFS