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 - 1 or + 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

题意:额,大概意思就是农场主要去找他自己的牛,农场主在N行,牛在K行,农场主一次可以走+1,-1和2*N这三种行进路线,每一种都会花费1分钟,那么问题来了,农场主最快要几分钟能够捉到自己的牛。

注意:要注意特殊数据的考虑 比如0-100000或者100000-0一道比较正常的广搜。

ac代码:

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
#define INF 0xfffffff
int vis[100010];
int k,n,cn[2]={1,-1},ans;
struct node{int n,step;
}a,temp;
int jud(int x){if(x>=0&&x<=100000&&vis[x]==0)return 1;return 0;
}
void bfs(int x){queue<node>q;a.n=x;a.step=0;q.push(a);vis[a.n]=1;int i;while(!q.empty()){a=q.front();q.pop();for(i=0;i<3;i++){if(i==2)temp.n=a.n*2;elsetemp.n=a.n+cn[i];temp.step=a.step+1;if(jud(temp.n)){vis[temp.n]=1;if(temp.n==k){if(ans>temp.step)ans=temp.step;    continue ;} q.push(temp);}}}}int main(){while(scanf("%d%d",&n,&k)!=EOF){memset(vis,0,sizeof(vis));ans=INF;if(n>=k)printf("%d\n",n-k);else{bfs(n);printf("%d\n",ans);    }}return 0;
}

POJ--3278 Catch That Cow相关推荐

  1. POJ 3278 Catch That Cow

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

  2. BFS POJ 3278 Catch That Cow

    题目传送门 1 /* 2 BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 3 */ 4 #include <cstdio> 5 #include <iostrea ...

  3. POJ 3278 Catch That Cow BFS

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

  4. POJ 3278 Catch That Cow(BFS)

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

  5. POJ 3278 Catch That Cow

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

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

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

  7. bfs+dfs分析----poj 3278 Catch That Cow

    题目详情 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 115430   Accepted:  ...

  8. poj 3278 Catch That Cow 广搜

    hdu 2717 Catch That Cow,题目链接 Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Jav ...

  9. poj 3278 Catch That Cow(广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45087   Accepted: 14116 ...

  10. POJ - 3278 Catch That Cow 简单搜索

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. ...

最新文章

  1. redis的主从复制,读写分离,主从切换
  2. 蒙克:云计算安全问题被夸大
  3. python 字符串大小写转换 其它不变_python字符串大小写如何转换
  4. 强烈推荐《价值:我对投资的思考》
  5. jekins创建ssh_linux – Jenkins SSH slave无法创建/ home // jenkins
  6. mysql 解决慢sql_MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法...
  7. hosts文件位置和修复hosts文件
  8. GPS定位RTK解决方案
  9. java软件前端开发_前端的编程软件哪些比较好用?
  10. 树莓派python蓝牙_在树莓派3B上做蓝牙音频
  11. python处理pdf实例_详解Python使用PDFMiner解析PDF实例
  12. nuxt.js项目打包上传服务器pm2启动各种问题
  13. 【支付】第三方支付收单机构
  14. 考研英语 - word-list-17
  15. MacOS系统通过命令行启动Chrome浏览器并添加启动参数
  16. Red5 流媒体技术(初级了解)
  17. adb关闭手机系统自动更新
  18. 【Hexo搭建个人博客】:yilia主题配置(四) - 分类管理
  19. 转:Eric Lippert:阅读代码真的很难
  20. mysql数据库基础知识点总结--看完即入门

热门文章

  1. 《Effective Java 3rd》读书笔记——对于所有对象都通用的方法
  2. Netty in action—EventLoop和线程模型
  3. 5月书讯丨​​《联邦学习》带队,10新书给你全方位的技能提升
  4. 挖掘11亿用户背后的产品逻辑之美
  5. linux/windows双系统安装、启动顺序设置及重新设置
  6. 【医疗影像处理】antspy数据读取与保存
  7. python的运行方式有哪两种_Python基础:Python运行的两种基本方式
  8. list 删除_算法面试题:一个List,要求删除里面的男生,不用Linq和Lamda,求各种解,并说明优缺点!...
  9. visio保存后公式变形_固体力学中的变形分析
  10. 苹果ipa签名解锁_朋友,iOS签名请了解一下