Problem 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

题意:在一数轴上,农夫位置是N,牛的位置是K,牛不会移动,农夫移动想要抓住牛。农夫有两种移动方式:从X到X-1或X+1,花费一分钟;从X到2*X,花费一分钟,求抓住牛的最小分钟数。

思路:bfs 搜索入门题

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 100001
#define MOD 2520
#define E 1e-12
using namespace std;
bool vis[N];
int dir[2]={-1,1};
struct node
{int x;int step;
}q[N*10];
void bfs(int n,int k)
{int head=1,tail=1;memset(vis,0,sizeof(vis));vis[n]=1;q[tail].x=n;q[tail].step=0;tail++;while(head<tail){int x=q[head].x;int step=q[head].step;int nx;if(x==k){printf("%d\n",step);break;}/*第一种走法*/for(int i=0;i<2;i++){nx=x+dir[i];if(0<=nx&&nx<N&&vis[nx]==0){vis[nx]=1;q[tail].x=nx;q[tail].step=step+1;tail++;}}/*第二种走法*/nx=x*2;if(nx>=0&&nx<N&&vis[nx]==0){vis[nx]=1;q[tail].x=nx;q[tail].step=step+1;tail++;}head++;}
}
int main()
{int n,k;scanf("%d%d",&n,&k);if(k<n){printf("%d",n-k);exit(0);}bfs(n,k);return 0;
}

Catch That Cow(POJ-3278)相关推荐

  1. poj 3278 Catch That Cow(广搜)

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

  2. Catch That Cow(广搜)

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

  3. Catch That Cow(详解)

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

  4. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  5. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  6. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  7. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  8. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  9. POJ 3278 Catch That Cow(BFS)

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

  10. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

最新文章

  1. linux %3e%3e 重定向,当我访问HTTPS时,网站保持重定向到HTTP,无明显原因
  2. php代码编辑器6,优秀的PHP代码编辑器_Blumentals Rapid PHP V13.6 免费版
  3. django mysql内存泄漏_Django的:使内存的关系,而不保存到数据库
  4. 如何构建基于.NET Core和云环境下的微服务技术体系?
  5. flush privileges
  6. PTA c语言 求幂级数展开的部分和
  7. matlab怎么复制相同的列,怎样将数组中某一列数据相同的行提取出来?
  8. 压缩UI深度的代码实现
  9. 非常经典的java编程题全集-共50题(11-30)
  10. YII2 blockui
  11. java实现的身份证照片脸部识别(头像截图) 以及OCR字体识别
  12. 同相放大、反向放大、差分放大电路的开环增益
  13. 工业革命4.0是光和算的革命,时代呼唤硬科技!
  14. poj1125 Stockbroker Grapevine Floyd算法
  15. 奖励稀疏_好奇心解决稀疏奖励任务
  16. SuMA++论文重点总结
  17. 中国计算机软件行业分析7---软件产业发展建议
  18. Firefox 账号信息同步/登录问题
  19. js 图片转base64的方式(两种)
  20. 【全球大赛】Haste Arcade在Game On竞赛中向游戏开发者提供5000美元奖金

热门文章

  1. 他用科技复活传统艺术:发现甲骨文的终极浪漫
  2. linux系统如何用root用户登陆,Linux用root账号创建一个新的登录账号的方法
  3. 想当年,我也是斩获20+大厂offer的收割机!
  4. 中国开发者数量全球第二,C 语言一跌再跌!GitHub 年度报告重磅发布
  5. 装了这几个插件后,我不得不给 IDEA 上个防沉迷
  6. 为什么阿里巴巴不允许使用Executors?
  7. 面试官:换人!他连进程线程协程这几个特点都说不出
  8. 面向犯罪编程,9名程序员锒铛入狱
  9. JEECG领取免费开发文档活动
  10. Linux 学习笔记_12_文件共享服务_2_FTP应用--vsftpd