Problem Descrption

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.

问题链接:http://poj.org/problem?id=3278

问题分析:搜索类问题,首先想到DFS和BFS两种,像这种很大的,如果用深搜很容易超时,所以明显是BFS,一开始写

为了记录步数使用了结构数组,然而这样会ML,这也给我提醒,以结构类型的队列可能内存较大,后来利用及数组记录父子节点的方式解决,可以建立数组ed[tx]=t,即记录tx的父节点为t,最后再回溯即可;

AC代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, k;
int vis[100005];
int ed[100005];
void bfs()
{queue<int>q;q.push(n);vis[n];while (!q.empty()){int t = q.front();if (t == k){return;}q.pop();int tx;for (int i = 0; i < 3; i++){if (i == 0) { tx = t + 1; }if (i == 1) { tx = t * 2; }if (i == 2) { tx = t - 1; }if (tx > 100000 || tx < 0 || vis[tx])continue;q.push(tx);ed[tx] = t;vis[tx]=1;}}
}
int main()
{while (cin >> n >> k){memset(vis, 0, sizeof(vis));bfs();int t=k,step=0;while (t!=n){t = ed[t];step++;}cout << step << endl;}
}

因使用结构队列而ML的代码:

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, k,min1=999999;
int vis[100005];
struct way
{int x;int step;
};
void bfs()
{queue<way>q;struct way a;a.x = n, a.step = 0;q.push(a);vis[a.x];while (!q.empty()){struct way t = q.front();if (t.x == k){if (t.step < min1)min1 = t.step;return;}q.pop();struct way tx;for (int i = 0; i < 3; i++){if (i == 0) { tx.x = t.x + 1; tx.step =t.step+1; }if (i == 1) { tx.x = t.x * 2; tx.step =t.step+1; }if (i == 2) { tx.x = t.x - 1; tx.step = t.step + 1; }if (tx.x > 100000 || tx.x < 0 || vis[tx.x])continue;q.push(tx);vis[tx.x];}}
}
int main()
{while (cin >> n >> k){memset(vis, 0, sizeof(vis));min1 = 9999999;bfs();cout << min1 << endl;}
}

POJ3278(BFS入门)相关推荐

  1. 【HDU 2612 Find a Way(BFS)】(兼BFS入门笔记)

    [HDU 2612 Find a Way(BFS)](兼BFS入门笔记) 原题入口: http://acm.hdu.edu.cn/showproblem.php?pid=2612 第一篇在CSDN的博 ...

  2. POJ3278(BFS)

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

  3. Find The Multiple BFS入门

    原题链接:1426 -- Find The Multiple 看不懂题意?嘿嘿,友情链接. 题意:输入一个n,就是找到一个由0,1组成的数M能够整除n,然后输出M. 老规矩直接上代码: BFS代码: ...

  4. Catch That Cow(农夫和牛)(BFS入门)(详解)

    题目: 题目大意: 输入n和k表示农夫和牛的位置,n和k在区间[0,100000]间 农夫有三种方式进行移动,每种方式需要的时间相同,都是一分钟 1.向前移动一米(+1) 2.向后移动一米(-1) 3 ...

  5. bjfu 1143 小蝌蚪安家(bfs入门)

    本人的第一题bfs搜索: 在一个矩形区域内,有些地方有水,有些地方没水.所有相邻的有水的地方会共同组成一个水洼,小蝌蚪想在这块区域中找到一个最大的水洼来安家. Input 有多组输入数据,每组第一行包 ...

  6. poj3278 【BFS】

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 97240   Accepted: 30519 ...

  7. poj3278 CatchThatCow bfs

    poj3278 BFS 里面稍微有一个坑的地方 这个地方一定要是100001而不可以是2*k #pragma warning(disable:4996) #include<iostream> ...

  8. python扫雷 广度优先_Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper)...

    Leetcode之广度优先搜索(BFS)专题-529. 扫雷游戏(Minesweeper) BFS入门详解:Leetcode之广度优先搜索(BFS)专题-429. N叉树的层序遍历(N-ary Tre ...

  9. (FZU-2285-迷宫寻宝)BFS最短路径问题

    题目网址:http://acm.fzu.edu.cn/problem.php?pid=2285 Problem 2285 迷宫寻宝 Accept: 276 Submit: 1040 Time Limi ...

最新文章

  1. 你的心思,我如何懂得?
  2. echo 和 var_dump
  3. 如何实现分类表统计数目和详情表数量同步
  4. Sql语句中IN和exists的区别及应用
  5. RPC与Restful比较
  6. linux sed命令_Linux sed命令用法与示例
  7. 问题五十六:怎么用ray tracing画参数方程表示的曲面(3)—— b-spline surface
  8. java课程讲解,Java基础教程详解:多线程(1)-----多线程概念
  9. 在C++Builder中使用OLE出现“类worksheet的paste方法无效”错误的一种解决方法
  10. Windows内核学习------双机调试的安装(物理机win10,虚拟机win7,虚拟机软件vmware)
  11. 非常规应用之PNP三级管倒置使用
  12. 传奇世界修改服务器时间,《传奇世界手游时长版》测试结束公告
  13. 【影像配准】配准之棋盘网格图(镶嵌图像)(附有 C++ 代码)
  14. 《“ 追梦人” 的逐梦路:探寻大学生创客群体的发展之道》
  15. 华为汽车BU业务布局及分析框架
  16. When Seawater Turns Sweet
  17. TI | TM4C系列单片机中断配置方法
  18. pancakeswap 前端源码编译及部署-linux
  19. WWDC 2015大会十大看点总结:Swift要开源了
  20. 四通道SOP封装晶体管输出光耦TLP291-4,LTV-247,PS2801-4

热门文章

  1. Rinne Loves Xor
  2. [HEOI2016TJOI2016]排序(二分+线段树)
  3. YBTOJ洛谷P2839:最大中位数(主席树、二分答案)
  4. 51nod1676-无向图同构【乱搞】
  5. jzoj1281-旅行【dp】
  6. POJ2524——宗教(Ubiquitous Religions)【图论,并查集】
  7. 【模拟】【递归】电子表格(jzoj 2127)
  8. 牛客网暑期ACM多校训练营(第九场)
  9. codeforces 932E Team Work 高等数学求导、dp
  10. IDE:Eclipse查看Servlet源码