题目网址:http://poj.org/problem?id=3278

题目:

Catch That Cow
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 92360   Accepted: 28999

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

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.

思路: 

很简单的一道BFS题。总共只有三种操作,每次都取队首元素分别进行三种操作,再对操作结果进行判断,筛去负数和超过100,000(边界值)的情况,其他情况都入队。

代码:

 1 #include <cstdio>
 2 #include <queue>
 3 #include <algorithm>
 4 using namespace std;
 5 int visited[100005];
 6 int n,k;
 7 queue<int>q;
 8 void bfs(){
 9     q.push(n);
10     while (!q.empty()) {
11         int x=q.front();q.pop();
12         if(x==k){
13             printf("%d\n",visited[x]-1);//初始次数为1,结果减去1
14             return ;
15         }
16         if(x-1>=0 && !visited[x-1]){//负数不考虑
17             q.push(x-1);
18             visited[x-1]=visited[x]+1;
19         }
20         if(x+1<=100000 && !visited[x+1]){//超过边界值的要筛去,否则会占用不必要的搜索时间
21             q.push(x+1);
22             visited[x+1]=visited[x]+1;
23         }
24         if(x*2<=100000 && !visited[x*2]){
25             q.push(x*2);
26             visited[x*2]=visited[x]+1;
27         }
28     }
29 }
30 int main(){
31     scanf("%d%d",&n,&k);
32     visited[n]=1;//初始位置也要赋值,防止第二次搜索到
33     bfs();
34     return 0;
35 }

转载于:https://www.cnblogs.com/uniles/p/7145860.html

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

  1. poj 3278 Catch That Cow (bfs)

    题目:http://poj.org/problem?id=3278 题意: 给定两个整数n和k 通过 n+1或n-1 或n*2 这3种操作,使得n==k 输出最少的操作次数 1 #include< ...

  2. poj 3278 Catch That Cow(广搜)

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

  3. HDU 2717 Catch That Cow(BFS)

    题目链接 好裸,BFS.杭电多组..2A.. 1 #include <stdio.h> 2 #include <string.h> 3 int p[100001],o[1000 ...

  4. C - Catch That Cow(BFS)

    找奶牛 有三种移动方式,作为BFS搜索的方向 利用 当 n > k 时只能向后走,优化结果 #include<iostream> #include<cstring> #i ...

  5. HDU2717 Catch That Cow(bfs)

    题意:农夫的牛跑了,给出农夫和牛在坐标轴上的位置n和k,农夫每次只能从点n移动到n-1.n+1或者n*2的位置.输出抓到牛所需要的最小移动次数. 思路:思路明显的bfs,每次搜索只按照这三种方式,标记 ...

  6. BFS POJ 3278 Catch That Cow

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

  7. POJ 3278 Catch That Cow

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

  8. POJ 3278 Catch That Cow BFS

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

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

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

最新文章

  1. 用ASP.Net(C#)连接Oracle数据库的方法
  2. 【 CodeForces - 799A 】Carrot Cakes(模拟,细节,有坑)
  3. 【英语学习】【Level 08】U05 Better option L1 Message sent
  4. jQuery 学习-样式篇(四):jQuery 设置和删除元素的属性
  5. Guava学习笔记(三):Preconditions优雅的检验参数
  6. 【python学习】装饰器@
  7. 教您简单几步实现工业树莓派正确安装RS232转USB驱动
  8. 服务器采集协议,H3C设备服务器采集参数认证过程(包含redfish和restfull协议)
  9. list对象转map
  10. 计算机主机放电操作,如何给主板CMOS放电的3种方法图文教程
  11. RuntimeError: iter() is only supported inside of tf.function or when eager execution is enabled.
  12. 等级保护二、三、四级内容及对比
  13. abupy文件结构功能
  14. 京东自动秒杀抢券php,【原创源码】【JavaScript】「京东超级百亿补贴」定时抢券脚本...
  15. MQL5-RPC来自 MQL5 的远程过程调用
  16. vue3项目实战---知乎日报----首页功能
  17. 安卓文字绘制和歌词器的简单实现
  18. 计算机二级c语言停考,计算机等级考试调整Fortran等语言停考
  19. 打造智慧幼儿园!学会一招,快速实现
  20. 小技巧:excel中月日年改年月日

热门文章

  1. [原]android2.3如何使用SharedPreferences存储字符串集合类型的元素
  2. 云计算背后的秘密(6)-NoSQL数据库的综述
  3. bitnami-redmine邮件告警配置
  4. 残差学习,152层网络,微软夺冠2015 ImageNet计算机视觉识别挑战
  5. 重新学习web后端开发-002-hello, world
  6. vmx转换ofv模板,导入esxi
  7. Python常用语句及流程控制
  8. C语言库函数的实战之一
  9. django Error: [Errno 10013]
  10. 浅谈Struts2和Sturts1的区别