相比于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. 在CentOS7下的OpenStack中配置使用Spice协议

    在CentOS7下的OpenStack中配置使用Spice协议 by 无若   1. 需要的包 在计算节点上 #yum install spice-html5   注意:使用yum安装spice-ht ...

  2. 作为有经验的程序员如果不懂Lambda表达式就说不过去了吧,建议收藏!!!

      最近刚好有空给大家整理下JDK8的特性,这个在实际开发中的作用也是越来越重了,本文重点讲解下Lambda表达式 Lambda表达式   Lambda 表达式,也可称为闭包,它是推动 Java 8 ...

  3. WPF(MVVM) 利用资源字典实现中英文动态切换

    1.首先新建两个字典文件en-us.xaml.zh-cn.xaml.定义中英文的字符串在这里面. 2.将两个资源字典添加到App.xaml中,这里注意下,因为两个字典中有同样字符,如果没有动态更改,默 ...

  4. SpringBoot开发七-开发注册功能

    需求介绍-开发注册功能 首先访问注册页面-点击顶部的链接,打开注册页面 提交注册数据 通过表单提交数据 服务端验证账号是否存在,邮箱是否已经注册 服务端发送激活邮件 激活注册账号 点击邮件中的链接,访 ...

  5. zzcms2020代码审计笔记

    在CNVD中看到zzcms后台sql注入漏洞: 漏洞细节未被公开,影响版本zzcms2020,本地搭建一套相同版本找一下漏洞触发点. 打开目录,后台ba***.php文件共有三个: 先看下bad.ph ...

  6. NOIP 模拟 $28\; \rm 客星璀璨之夜$

    题解 \(by\;zj\varphi\) 概率与期望,考虑 \(\rm dp\) 设 \(dp_{i,j}\) 为消除 \(i-j\) 这一段行星的期望,转移: 枚举 \(k\) 为当前状态下第一个撞 ...

  7. flutter获取状态栏高度及安全区域

    获取状态栏高度: final double statusBarHeight = MediaQuery.of(context).padding.top; 所谓安全区域,就是适配现在一些刘海屏之类的非常规 ...

  8. C# 高级进阶(一)

    产品是怎样开发出来的 首先了解--产品MVP(Minimum Viable Product)的概念 最小可行产品--意思即开发最初应该是根据参品预期,最快构建出一个符合预期可行的功能合集,后期再通过用 ...

  9. 【mysql】用户和权限管理

    1.用户管理 相关命令如下 命令 描述 备注 create user zhang3 identified by '123123'; 创建名称为zhang3 的用户,密码设为123123:   sele ...

  10. C++ 计算MD5

    头文件: #pragma once #ifndef MD5_H #define MD5_H #include <string> #include <fstream> /* Ty ...