Catch That Cow

Time Limit:2000MS  Memory Limit:65536K
Total Submit:118 Accepted:30

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.

Source

#include<iostream> #include<queue> using namespace std; #define M 100001 #define INF 10000000 struct POINT { int pos; int step; }now,next; queue<POINT>Q; bool visited[M]; int n,k; int bfs() { while(!Q.empty()) Q.pop(); now.pos=n; now.step=0; visited[now.pos]=true; Q.push(now); while(!Q.empty()) { now=Q.front(); Q.pop(); next=now; for(int i=0;i<3;i++) { if(i==0) next.pos=now.pos+1; if(i==1) next.pos=now.pos-1; if(i==2) next.pos=now.pos*2; next.step=now.step+1; if(next.pos==k) return next.step; if(next.pos<0||next.pos>M) continue; if(!visited[next.pos]) { visited[next.pos]=true; Q.push(next); } } } return INF; } int main() { while(cin>>n>>k) { memset(visited,false,sizeof(visited)); if(n<k) cout<<bfs()<<endl; if(n==k) cout<<0<<endl; if(n>k) cout<<n-k<<endl; } }

Catch That Cow相关推荐

  1. C - Catch That Cow POJ - 3278

    C - Catch That Cow POJ - 3278 首先是大暴搜+最优化剪枝(当当前搜索情况不如已有答案优时,返回) #include<cstdio> #include<al ...

  2. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35043   Accepted: 10800 ...

  3. POJ 3278 Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 30924   Accepted: 9536 D ...

  4. POJ 3278 Catch That Cow BFS

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32071   Accepted: 9866 D ...

  5. POJ 3278 Catch That Cow(BFS)

    题目网址:http://poj.org/problem?id=3278 题目: Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Tot ...

  6. 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)

    链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...

  7. POJ3278——Catch That Cow

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 114140   Accepted: 35715 ...

  8. 动态规划训练13 [Catch That Cow poj3278]

    Catch That Cow POJ - 3278 这道题我看大家用的方法都是bfs搜索,为什么在我看来这就是一个动态规划的题目啊啊啊啊啊啊啊 dp[x]表示从N出发到x所需要的最小时间 那么得到如下 ...

  9. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  10. Catch That Cow【广搜】

    Catch That Cow POJ - 3278 Farmer John has been informed of the location of a fugitive cow and wants ...

最新文章

  1. PHP自动加载类—__autoload()和spl_autoload_register()
  2. 深度学习笔记 第四门课 卷积神经网络 第二周 深度卷积网络:实例探究
  3. android 串口一直打开_实例 | 使用Python串口实时显示数据并绘图
  4. 【BZOJ 1449】 1449: [JSOI2009]球队收益 (最小费用流)
  5. hdu4064 三进制状态压缩 好题!
  6. python3安装步骤-Mac安装python3的方法步骤
  7. Scrapy爬取小说简单逻辑
  8. uva 11234 Expressions
  9. spring中使用quartz(注解版)
  10. 一位原码的乘法规则_原码一位乘法的实现算法
  11. 图的深度优先遍历算法
  12. html5网页制作心得体会,学习网页制作心得体会范文3篇
  13. ACwing166数独与183靶形数独
  14. Python人脸识别黑科技(二):教你使用python+Opencv完成人脸解锁
  15. 小学计算机网络信息安全教案,黑教版信息技术五年级上册第十五课《网络信息安全》教案.doc...
  16. c++笔记③1109
  17. 3.2 从关系型数据库中查询数据
  18. [墨者学院] 日志文件分析溯源(脱裤的IP地址)
  19. TOP Network 2019年度回顾:积蓄力量,再创辉煌
  20. C语言项目二:电阻并联

热门文章

  1. 智云通CRM:销售的黄金法则,尊重客户的意见
  2. 基于SPM12对fMRI数据进行预处理及其批处理
  3. 准备软硬件openGauss安装环境
  4. Win10系统中临时文件夹位置及临时文件的删除
  5. 全球及中国导电聚合物(CP)型电子鼻发展前景与投资战略分析报告2022~2028年
  6. 专门为u盘设计的linux系统
  7. 画中画怎么制作?教你如何录制画中画视频,图文教学
  8. STD高级语言程序设计第二天(满分答案)
  9. #include tchar.h 是什么意思
  10. Linaro Ubuntu for Arndale Octa Broad Exynos 5420开发板,启动系统sd卡的制作。