相比于POJ2251的三维BFS,这道题做法思路完全相同且过程更加简单,也不需要用结构体,check只要判断vis和左右边界的越界情况就OK。

记得清空队列,其他没什么好说的。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std; const int maxn=100001; bool vis[maxn];
int step[maxn];
queue <int> q; int bfs(int n,int k)
{
int head,next;
q.push(n);
step[n]=0;
vis[n]=true;
while(!q.empty())
{
head=q.front();
q.pop();
for(int i=0;i<3;i++)
{
if(i==0) next=head-1;
else if(i==1) next=head+1;
else next=head*2;
if(next<0 || next>=maxn) continue;
if(!vis[next])
{
q.push(next);
step[next]=step[head]+1;
vis[next]=true;
}
if(next==k) return step[next];
}
}
}
int main()
{
int n,k;
while(cin>>n>>k)
{
memset(step,0,sizeof(step));
memset(vis,false,sizeof(vis)); while(!q.empty()) q.pop();
if(n>=k) printf("%d\n",n-k);
else printf("%d\n",bfs(n,k));
}
return 0;
}

POJ——3278 Catch That Cow(BFS队列)的更多相关文章

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

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

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

  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搜索)

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

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

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

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

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

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

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

  9. POJ - 3278 Catch That Cow bfs 线性

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

  10. BFS POJ 3278 Catch That Cow

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

随机推荐

  1. [C++]-set 集合

    set集合用于存放一组无重复的元素.由于集合本身是有序的,所以可以高效地查找指定元素,也可以方便地得到指定大小范围地元素在容器中所处区间. 代码 #include<iostream> #i ...

  2. RationalRose使用

    UML 概述 什么是 UML? Unified Modeling Language (UML)又称统一建模语言或标准建 模语言,是始于 1997 年一个 OMG 标准,它是一个支持模型化 和软件系统开 ...

  3. SpringMVC学习06(JSON)

    6.Json 6.1 什么是JSON? JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式,目前使用特别广泛. 采用完全独立于编程语言的文本 ...

  4. 一键部署lamp脚本

    #!/bin/bash systemctl stop firewalld systemctl disable firewalld setenforce 0 #-------Apache------ # ...

  5. Java服务突然失败:A fatal error has been detected by the Java Runtime Environment的总结

    服务启动以后过段时间自动失败:A fatal error has been detected by the Java Runtime Environment 控制台中的错误信息 A fatal err ...

  6. perfdog的基本使用

    perfdog的下载安装就不做过多介绍了,直接进入正题,首先介绍下perfdog页面的结构 如下图 1.显示连接的设备 2.选择需要测试的app 3.device tab下显示设备的基本信息,sett ...

  7. Mysql使用存储过程快速添加百万数据

    前言 为了体现不加索引和添加索引的区别,需要使用百万级的数据,但是百万数据的表,如果使用一条条添加,特别繁琐又麻烦,这里使用存储过程快速添加数据,用时大概4个小时. 创建一个用户表 CREATE TA ...

  8. Redis分布式锁的原理和实现

    前言 我们之前聊过redis的,对基础不了解的可以移步查看一下: 几分钟搞定redis存储session共享--设计实现:https://www.cnblogs.com/xiongze520/p/10 ...

  9. Avro使用手册

    1. Overview Data serialization is a technique of converting data into binary or text format. There a ...

  10. selenium处理:您的连接不是私密连接的网站(https ssl 证书)

    当前我们遇到这样的问题,就要通过代码的层面去解决 requests的话verify参数设置为False selenium的话添加参数:--ignore-certificate-errors 测试代码: ...