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

Catch That Cow(POJ3278)

BFS入门题,然鹅我还是WA了四五发,因为没注意,位置0是可以访问的。再者就是初始位置在push之后,要标记为已经访问。

图片挺不错,我们地大(武汉)的旖旎风光,放松一下。

题目链接:POJ3278

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.

题意:

这个题建立模型,可以理解为在0-maxn的x轴上,农夫在N点,奶牛在K点,有一下三种方式农夫可以改变自己的位置,每次改变花费一分钟,求到达奶牛的位置的最短时间。

题解:

这个题求最短路,可以想到用BFS。

这里复习一下BFS,BFS本质就是搜索,在搜索过程中,为每一个节点分层,到达目标节点,结束搜索,可以确保找到最优解。由于BFS保存结点数目较多,采用队列实现。这个题我的男神郭炜老师的课件用一个结构体保存每一个点的位置和到达该位置的步数,如下,简单明了。

struct node{
int step,pos;
node(int pos=0,int step=0) : pos(pos),step(step){}
};

也可只用一个vis数组,用于判断是否被访问和当前的步数。基本的BFS题。

还有一点对于BFS的多组输入的题,每次vis数组要清空,q队列清空。

代码

#include<cstdio>
#include<cstring>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
//节点拓展,分层,判重。确保找到最优解,但保存节点较多,多数节点需要保存,队列 typedef long long ll;
const int maxn = 100000;
int vis[500000 + 10];
void Bfs(int n, int k)
{
memset(vis, 0, sizeof(vis));
queue<int>q;
while (!q.empty()) q.pop(); //注意调用前要先清空
q.push(n);
vis[n] = 1;
while (!q.empty())
{
int u = q.front(); q.pop();
if (u== k)//找到目标,结束搜索
{
cout << vis[k]-1 << endl;
return;
}
else {
//这里是大于等于。
if (u - 1 >= 0 && !vis[u - 1]) {
q.push(u - 1);
vis[u - 1] = vis[u]+ 1;//上一个节点的下一层
}
if (u + 1<=maxn && !vis[u +1]) {
q.push(u +1);
vis[u+1] = vis[u] + 1;
}
if (2 * u <= maxn && !vis[2 *u])
{
q.push(2 * u);
vis[2 * u] = vis[u] + 1;
} }
} }
int main()
{ int n, k;
while (cin >> n >> k)
{
if (n >= k)cout << n - k << endl;//可以用来加快运行,小的剪枝
else
Bfs(n, k);
}
return 0;
}

本题结束啦,ACM之路还没结束......

POJ 3278 Catch That Cow[BFS+队列+剪枝]的更多相关文章

  1. poj 3278 Catch That Cow(bfs+队列)

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

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

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

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

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

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

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

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

  6. poj 3278 Catch That Cow bfs

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

  7. POJ 3278 Catch That Cow bfs 难度:1

    http://poj.org/problem?id=3278 从n出发,向两边转移,为了不使数字无限制扩大,限制在2*k以内, 注意不能限制在k以内,否则就缺少不断使用-1得到的一些结果 #inclu ...

  8. POJ - 3278 Catch That Cow bfs 线性

    #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> usi ...

  9. BFS POJ 3278 Catch That Cow

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

随机推荐

  1. 逼格高又实用的Linux高级命令,开发运维都要懂!

    在运维的坑里摸爬滚打好几年了,我还记得我刚开始的时候,我只会使用一些简单的命令,写脚本的时候,也是要多简单有多简单,所以有时候写出来的脚本又长又臭. 像一些高级点的命令,比如说 Xargs 命令.管道 ...

  2. 更新ruby:Error running 'requirements_osx_brew_update_system ruby-2.4.1报错解决

    更新ruby时,报错: Failed to update Homebrew, follow instructions here: https://github.com/Homebrew/homebre ...

  3. SciTe设置

    对于新手来说,如果没有正确的配置,它就不是那么好使,比如选择中文时候出现乱码,缩进也不是你想象中的样子. 由于配置参数不是采用图形界面,而且出看配置代码会比较混乱,所以大家要睁大眼睛好好看咯- 程序中 ...

  4. golang 面向对象编程

    概述 Golang语言的面向对象与c++,py等语言有所不同,是由于Golang不支持继承:与上述支持聚合和继承的面向对象的语言不同,Golang只支持聚合(也叫做组合)和嵌入.聚合和嵌入的区别: t ...

  5. 【Android】解析Paint类中MaskFilter的使用

    目录结构: contents structure [+] EmbossMaskFilter BlurMaskFilter MaskFilter可以用来指定画笔的边缘效果.如果引用开启硬件加速的话,那么 ...

  6. 搭建一个免费的,无限流量的Blog----github Pages和Jekyll入门[zz]

    喜欢写Blog的人,会经历三个阶段. 第一阶段,刚接触Blog,觉得很新鲜,试着选择一个免费空间来写. 第二阶段,发现免费空间限制太多,就自己购买域名和空间,搭建独立博客. 第三阶段,觉得独立博客的管 ...

  7. [转]RabbitMQ的安装与客户端的简单实用

    原文地址:http://www.cnblogs.com/yangh965/p/5862347.html 本文主要内容是RabbitMQ的安装步骤[Windows系统与linux上的安装]及客户端的简单 ...

  8. 用HTML5+JS开发跨平台的桌面应用

    通过Node.js和WebKit技术的融合,开发者可以用HTML5技术编写UI,同时又能利用Node.js平台上众多library访问本地OS的能力,最终达到用Web技术就可以编写桌面应用的目的. 选 ...

  9. Vue获取DOM元素的属性值

    项目中需要做一个小弹层,如下图: 我需要知道点击元素距离顶部的值,再计算弹层的top值,如下图: 在vue中如何获取到DOM元素距离窗口顶部的值呢? 1.通过$event获取 html: <di ...

  10. ffmpeg 移植到 android 并使用

    同步更新至个人blog:http://dxjia.cn/2015/07/ffmpeg-porting-to-android/ 空闲做了个小应用,从视频里截图,然后再将截图拼接为一个gif动画: 起初使 ...