Catch That Cow

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

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

第一种:含标记数组

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int n, k,ans,step[200010],book[200010]; //wa了好几次,这里一定要大一点 ,step:步数 book:标记走没走
void bfs(int a, int b) {
ans = 0;
book[a] = 1;
queue<int >q;
q.push(a);
while (!q.empty()) {
int x = q.front();
//cout <<x<<" "<<b<<"\n";
q.pop();
if( x== b)break; if (x * 2 <= 200010 && x * 2 >= 0 && book[x * 2] == 0) { //判断边界,判断走没走,
book[2 * x] = 1;
q.push(x * 2);
step[x * 2] = step[x] + 1;
}
if (x + 1 <= 200010 && x + 1 >= 0 && book[x + 1] == 0) { //判断边界,判断走没走,
book[1 + x] = 1;
q.push(x +1);
step[x + 1] = step[x] + 1;
}
if (x - 1 <= 200010 && x - 1 >= 0 && book[x - 1] == 0) { //判断边界,判断走没走,
book[x - 1] = 1;
q.push(x -1);
step[x - 1] = step[x] + 1;
}
}
}
int main() {
while (cin >> n >> k) {
memset(step, 0, sizeof(step)); //不要忘记初始化
memset(book, 0, sizeof(book));
if (n >= k)cout << n - k << endl; //n不大于k的话 就是n-k了
else {
bfs(n, k);
cout << step[k] << endl;
}
}
return 0;
}

第二种:

不加标记数组,思考了两天了,我一直认为可以不加标记数组,但是提交就wa了,找了两天原因没找到,终于发现了,是我判断的顺序不对,一定要最后判读2*x,否则先判断2*i,后边会越来越大,所以你就不好把握最大值了,并且容易超时,所以先判断x-1  

判断顺序很重要

#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
int n, k,step[200010];
int bfs(int a, int b) {
queue<int >q; //初始化
memset(step, 0, sizeof(step));
q.push(a); while (!q.empty()) {
int x = q.front();
q.pop(); if (x == b)break; //找到就终止 if (x - 1 <= 200010 && x - 1 >= 0 && step[x -1] == 0) { //这三个顺序很重要,
q.push(x - 1);
step[x - 1] = step[x] + 1;
}
if (x + 1 <= 200010 && x + 1 >= 0 && step[x +1] == 0) {
q.push(x + 1);
step[x + 1] = step[x] + 1;
}
if (x * 2 <= 200010 && x * 2 >= 0 && step[x * 2]==0) { //这个一定要放在最后边
q.push(x * 2);
step[x * 2] = step[x] + 1;
}
}
return step[b];
}
int main() {
while (cin >> n >> k) { if (n >= k) //因为n减小只能-1,所以直接输出就可以
cout << n - k << endl;
else
cout << bfs(n, k)<< endl;
}
return 0;
}
 

Catch That Cow:BFS:加标记数组:不加标记数组的更多相关文章

  1. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  2. poj 3278 Catch That Cow (bfs搜索)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 46715   Accepted: 14673 ...

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

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

  4. POJ 3278 Catch That Cow[BFS+队列+剪枝]

    第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...

  5. POJ3278 Catch That Cow —— BFS

    题目链接:http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total S ...

  6. POJ3278——Catch That Cow(BFS)

    Catch That Cow DescriptionFarmer John has been informed of the location of a fugitive cow and wants ...

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

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

  8. POJ - 3278 Catch That Cow BFS求线性双向最短路径

    Catch That Cow Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  9. catch that cow (bfs 搜索的实际应用,和图的邻接表的bfs遍历基本上一样)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38263   Accepted: 11891 ...

  10. POJ3278 Catch That Cow(BFS)

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

随机推荐

  1. 二叉查找树(二叉排序树)(C语言)

    #include<stdio.h> #include "fatal.h" struct TreeNode; typedef struct TreeNode *Posit ...

  2. $(document).ready(function(){})与window.load

    $(document).ready(function(){ //to do something}) 是当文档全部加载完全的时候触发,包括img也加载完成但是相关的文件没有下载下来,能同时编写多个 wi ...

  3. ajax 动态载入html后不能执行其中的js解决方法

    事件背景 有一个公用页面需要在多个页面调用,其中涉及到部分js已经写在了公用页面中,通过ajax加载该页面后无法执行其中的js. 解决思路 1. 采用附加一个iframe的方法去执行js,为我等代码洁 ...

  4. Wavesurfer.js音频播放器插件的使用教程

    Wavesurfer.js是一款基于HTML5 canvas和Web Audio的音频播放器插件,本文主要记录它及其视觉效果插件Regions插件的使用方法. 1.创建实例 引入插件 import W ...

  5. ESP8266传送文件设置和操作

    问题:ESP8266不能被识别为USB盘,也不能通过串口传送文件,只能通过wifi传送文件,需要事前在串口连接后的repl状态下,用命令行方式配置网络参数,之后才可以用wifi传送文件(webrepl ...

  6. python自动化学习

    1.环境搭建 1.1 下载或拷贝整个PyAuthoTest到指定的目录,如D:\PyAuthoTest 1.2 安装Python2.7以及需要使用到的安装包列表如下 requests-2.10.0.t ...

  7. 网站sql注入漏洞修复方案之metinfo 6.1.0系列

    近日,我们SINE安全对metinfo进行网站安全检测发现,metinfo米拓建站系统存在高危的sql注入漏洞,攻击者可以利用该漏洞对网站的代码进行sql注入攻击,伪造恶意的sql非法语句,对网站的数 ...

  8. mysql 日志log

    my.ini log-error=D:/phpStudy/PHPTutorial/MySQL/log/error.loglog=D:/phpStudy/PHPTutorial/MySQL/log/my ...

  9. homebrew 使用心得

    ''' 安装anaconda 安装命令: brew search anaconda brew cask install anaconda 添加环境变量: vi ~/.bash_profile expo ...

  10. Node.js 引用 gm 包错误 Error: Could not execute GraphicsMagick/ImageMagick

    今天在学习前后台图像剪切时,下载了有图片剪切瑞士军刀之称的 GraphicsMagick. 给 gm.exe 配置了环境变量,在 npm 下好了 gm 的模块,但是运行却出现了错误. 错误如图: [E ...