http://poj.org/problem?id=3278
Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 146388   Accepted: 44997

Description

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.
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long ll ;
int vis[];
int ans[];
int n , m ; void bfs(int b)
{
queue<int>q;
memset(vis , , sizeof(vis));
memset(ans , , sizeof(ans));
if(n < m)
{ q.push(b);
vis[b] = ;
ans[b] = ;
while(!q.empty())
{ int x = q.front();
if(x == m)
{
printf("%d\n" , ans[m]);
break ;
}
q.pop() ;
if(x >= && x <= && !vis[x*])
{
vis[*x] = ;
ans[*x] += vis[x] + ans[x];
q.push(*x);
}
if(x >= && !vis[x+])
{
vis[x+] = ;
ans[x+] += vis[x] + ans[x];
q.push(x+);
}
if(x - >= && !vis[x-])
{
vis[x-] = ;
ans[x-] += vis[x] + ans[x];
q.push(x-);
}
} }
else
{
printf("%d\n" , n - m);
} } int main()
{ while(~scanf("%d%d" , &n , &m))
{
bfs(n);
} return ;
}

最新文章

  1. pdo的使用
  2. msChart组件安装与编程
  3. hyper-v虚拟化管理
  4. python基础——模块
  5. sqlserver 2000事务复制问题
  6. .Net 使用文件上传控件FileUpload上传图片
  7. C++中关于类型转换的问题讨论
  8. Android 调用系统的分享[完美实现同一时候分享图片和文字]
  9. MVC之ActionResult
  10. Java IO读写中文各种乱码问题 【转】
  11. MySQL优化GROUP BY-松散索引扫描与紧凑索引扫描
  12. c3p0、dbcp、tomcat jdbc pool 连接池配置简介及常用数据库的driverClass和驱动包
  13. 从PRISM开始学WPF(六)MVVM(三)事件聚合器EventAggregator?
  14. css垂直居中方法总结
  15. redis高可用(主从复制)
  16. nginx 返回数据不完整
  17. redis 在 php 中的应用(事务 [ Transaction ] 篇)
  18. java 集合(四)HashSet 与 LinkedHashSet
  19. 30-算法训练 最短路 spfa
  20. 2018/03/09 每日一学PHP 之 require_once require include include_once 包含文件的区别

热门文章

  1. 你真的了解iOS的深浅拷贝吗?
  2. JSZip
  3. MVC项目集成swagger
  4. R语言——ggplot2补充知识点
  5. LinuxC语言实现ATM取款机实验Socket
  6. [POJ3694]Network(Tarjan,LCA)
  7. VPS 安装MySQL
  8. java中的Excel导出功能
  9. 四种会话跟踪技术以及jstl介绍
  10. Spring---异步消息