题目传送门

 /*
BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstring>
using namespace std; const int MAXN = 1e6 + ;
const int INF = 0x3f3f3f3f;
int n, m;
int d[MAXN];
bool vis[MAXN]; void BFS(void)
{
memset (vis, , sizeof (vis));
for (int i=; i<=1e6; ++i) d[MAXN] = ; queue<int> q;
q.push (n); d[n] = ; vis[n] = true; while (!q.empty ())
{
int x = q.front (); q.pop ();
if (x == m) break; int xl = x - ; int xr = x + ; int x2 = x * ; if (xl >= && !vis[xl])
{
q.push (xl); d[xl] = d[x] + ;
vis[xl] = true;
}
if (xr <= 1e6 && !vis[xr])
{
q.push (xr); d[xr] = d[x] + ;
vis[xr] = true;
}
if (x2 <= 1e6 && !vis[x2])
{
q.push (x2); d[x2] = d[x] + ;
vis[x2] = true;
}
} } int main(void) //POJ 3278 Catch That Cow
{
//freopen ("POJ_3278.in", "r", stdin); while (scanf ("%d%d", &n, &m) == )
{
BFS ();
printf ("%d\n", d[m]);
} return ;
}

BFS POJ 3278 Catch That Cow的更多相关文章

  1. 搜索 || BFS || POJ 3278 Catch That Cow

    农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...

  2. POJ 3278 Catch That Cow(赶牛行动)

    POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Farmer J ...

  3. POJ 3278 Catch That Cow (附有Runtime Error和Wrong Answer的常见原因)

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

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

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

  5. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

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

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

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

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

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

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

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

随机推荐

  1. [Effective JavaScript 笔记]第43条:使用Object的直接实例构造轻量级的字典

    js对象的核心是一个字符串属性名与属性值的映射表.使用对象实现字典易如反掌,字典是可变长的字符串与值的映射集合. for...in js提供了枚举一个对象属性名的利器--for...in循环. var ...

  2. nginx(三)初步搭建nginx虚拟主机

    上面就是nginx基于域名.ip访问的配置,掌握住格式,就很好配置了. 一.基于域名的虚拟主机的配置:1.我们在此复习一下DNS的配置:[root@mgmserver /]# hostnamemgms ...

  3. MD(d)、MT(d)编译选项的区别

    1.编译选项的位置 以VS2005为例,这样子打开: 1)         打开项目的Property Pages对话框 2)         点击左侧C/C++节 3)         点击Code ...

  4. 【leetcode】Sort List

    Sort List Sort a linked list in O(n log n) time using constant space complexity.   需要采用归并排序对链表进行操作. ...

  5. kettle job通过javascript进行循环控制

    任何一种编程语言都少不了循环,kettle中的job也一样.那么kettle中的job是怎么通过JavaScript来达到类似于编程语言中的for循环呢? var max = parent_job.g ...

  6. 转mysql复制主从集群搭建

    最近搭了个主从复制,中间出了点小问题,排查搞定,记录下来 1环境:虚拟机:OS:centos6.5Linux host2 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 ...

  7. mysql show

    1.show命令语法 SHOW {BINARY | MASTER} LOGS SHOW BINLOG EVENTS [IN 'log_name'] [FROM pos] [LIMIT [offset, ...

  8. Avisynth + DirectShow + WebCamera 实现Avisynth脚本访问摄像头

    准备工作:需要以下三种软件 1.Avisynth_258 安装文件和源码下载地址(Avisynth_258.exe 4.2 MB) http://sourceforge.net/projects/av ...

  9. August 4th, 2016, Week 32nd, Thursday

    How does the world look through your eyes? 你眼中的世界是什么样的呢? This morning I saw a girl that is just the ...

  10. Girls and Boys(poj 1466)

    题目描述: 给出一系列男女配对意愿信息.求一个集合中的最大人数,满足这个集合中两两的人不能配对. /* 二分图的最大独立集 因为没有给出具体的男生和女生,所以可以将数据扩大一倍,即n个男生,n个女生, ...