链接:

http://poj.org/problem?id=3278

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 62113   Accepted: 19441

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

  1. 5 17

Sample Output

  1. 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.

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6. #include <queue>
  7.  
  8. using namespace std;
  9.  
  10. #define N 110000
  11.  
  12. struct node
  13. {
  14. int x, step;
  15. };
  16.  
  17. int s, e;
  18. bool vis[N];
  19.  
  20. int BFS(int s)
  21. {
  22. node p, q;
  23. p.x = s, p.step = ;
  24.  
  25. memset(vis, false, sizeof(vis));
  26. vis[s] = true;
  27. queue<node>Q;
  28. Q.push(p);
  29.  
  30. while(Q.size())
  31. {
  32. p = Q.front(), Q.pop();
  33.  
  34. if(p.x == e) return p.step;
  35.  
  36. for(int i=; i<; i++)
  37. {
  38. if(i==)
  39. q.x = p.x + ;
  40. else if(i==)
  41. q.x = p.x - ;
  42. else if(i==)
  43. q.x = p.x * ;
  44.  
  45. q.step = p.step + ;
  46. if(q.x>= && q.x<N && !vis[q.x])
  47. {
  48. Q.push(q);
  49. vis[q.x] = true;
  50. }
  51. }
  52. }
  53.  
  54. return -;
  55. }
  56.  
  57. int main()
  58. {
  59. while(scanf("%d%d", &s, &e)!=EOF)
  60. {
  61. int ans = BFS(s);
  62.  
  63. printf("%d\n", ans);
  64. }
  65. return ;
  66. }

(广搜)Catch That Cow -- poj -- 3278的更多相关文章

  1. catch that cow POJ 3278 搜索

    catch that cow POJ 3278 搜索 题意 原题链接 john想要抓到那只牛,John和牛的位置在数轴上表示为n和k,john有三种移动方式:1. 向前移动一个单位,2. 向后移动一个 ...

  2. Catch That Cow POJ - 3278 [kuangbin带你飞]专题一 简单搜索

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

  3. Catch That Cow POJ - 3278 bfs map超时,短路判断顺序。

    题意:可以把n边为n+1,n-1,n*2问从n到k的最少变化次数. 坑:标题写了.有点不会写bfs了... ac代码 #define _CRT_SECURE_NO_WARNINGS #include& ...

  4. C - Catch That Cow POJ - 3278

    //标准bfs #include <iostream> #include <cstdio> #include <algorithm> #include <cm ...

  5. kuangbin专题 专题一 简单搜索 Catch That Cow POJ - 3278

    题目链接:https://vjudge.net/problem/POJ-3278 题意:人可以左移动一格,右移动一格,或者移动到当前位置两倍下标的格子 思路:把题意的三种情况跑bfs,第一个到达目的地 ...

  6. (广搜)Dungeon Master -- poj -- 2251

    链接: http://poj.org/problem?id=2251 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2137 ...

  7. poj 3278:Catch That Cow(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

  8. poj 3278 Catch That Cow (广搜,简单)

    题目 以前做过,所以现在觉得很简单,需要剪枝,注意广搜的特性: 另外题目中,当人在牛的前方时,人只能后退. #define _CRT_SECURE_NO_WARNINGS //这是非一般的最短路,所以 ...

  9. POJ 3278 Catch That Cow(BFS,板子题)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 88732   Accepted: 27795 ...

随机推荐

  1. centos6.6 myphpadmin

    基本环境为:Centos6.6+Apache2.2.15+php5.3.3+Mysql5.1.73 开始下载了网站上最新版本myPhpAdmin4.3.8安装后打开浏览器为空白页,后百度后都讲是与PH ...

  2. linux Posix 信号量 三 (经典例子)

    本文将阐述一下信号量的作用及经典例子,当中包括“<越狱>寄信”,“家庭吃水果”,“五子棋”,“接力赛跑”,“读者写者”,“四方恋爱”等 首先,讲 semWait操作(P操作)和semSig ...

  3. 马士兵Spring-AOP-XML配置(2)

    一. UserDAO.java: package com.cy.dao; import com.cy.model.User; public interface UserDAO { public voi ...

  4. jQuery的文档操作

    1.插入操作 一.父元素.append(子元素) 追加某元素 父元素中添加新的元素 var oli = document.createElement('li'); oli.innerHTML = '哈 ...

  5. 文件os.path相关方法

    #!/usr/bin/python3# -*- coding: utf-8 -*-# @Time    : 2018/6/13 15:03# @File    : abspath_1.py impor ...

  6. 分布式锁实践(一)-Redis编程实现总结

    写在最前面 我在之前总结幂等性的时候,写过一种分布式锁的实现,可惜当时没有真正应用过,着实的心虚啊.正好这段时间对这部分实践了一下,也算是对之前填坑了. 分布式锁按照网上的结论,大致分为三种:1.数据 ...

  7. 225. Implement Stack using Queues + 232. Implement Queue using Stacks

    ▶ 栈和队列的相互表示.发现内置的队列和栈结构都十分高效,相互表示后性能损失都很小. ▶ 第 225 题,用队列实现栈 ● 自己的代码,3 ms,单队列实现,入栈 O(1),读取栈顶元素 O(n),出 ...

  8. 如何优化Java垃圾回收-zz

    为什么需要优化GC 或者说的更确切一些,对于基于Java的服务,是否有必要优化GC?应该说,对于所有的基于Java的服务,并不总是需要进行GC优化,但前提是所运行的基于Java的系统,包含了如下参数或 ...

  9. MVC4 AspNet MVC下的Ajax / 使用微软提供的Ajax请求脚本 [jquery.unobtrusive-ajax.min.js]

    源码参考:链接:http://pan.baidu.com/s/1pKhHHMj  密码:mkr4 1:新建-->项目-->Web-->ASP.NET MVC 4 Web 应用程序.命 ...

  10. 探究算子find_shape_model中参数MaxOverlap的准确意思

    基于形状的模板查找算子: find_shape_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxO ...