BFS POJ 3278 Catch That Cow
/*
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的更多相关文章
- 搜索 || BFS || POJ 3278 Catch That Cow
农夫在x位置,下一秒可以到x-1, x+1, 2x,问最少多少步可以到k *解法:最少步数bfs 要注意的细节蛮多的,写在注释里了 #include <iostream> #include ...
- POJ 3278 Catch That Cow(赶牛行动)
POJ 3278 Catch That Cow(赶牛行动) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 Farmer J ...
- 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 ...
- POJ 3278 Catch That Cow(BFS,板子题)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 88732 Accepted: 27795 ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- POJ 3278 Catch That Cow[BFS+队列+剪枝]
第一篇博客,格式惨不忍睹.首先感谢一下鼓励我写博客的大佬@Titordong其次就是感谢一群大佬激励我不断前行@Chunibyo@Tiancfq因为室友tanty强烈要求出现,附上他的名字. Catc ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- 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 ...
随机推荐
- Word Amalgamation(枚举 + 排序)
Word Amalgamation Time Limit: 1 Sec Memory Limit: 64 MB Submit: 373 Solved: 247 Description In mil ...
- Spring常用的接口和类(二)
七.BeanPostProcessor接口 当需要对受管bean进行预处理时,可以新建一个实现BeanPostProcessor接口的类,并将该类配置到Spring容器中. 实现BeanPostPro ...
- Spring AOP使用整理:自动代理以及AOP命令空间
三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...
- [Effective JavaScript 笔记] 第8条:尽量少用全局对象
初学者容易使用全局变量的原因 创建全局变量毫不费力,不需要任何形式的声明(只要在非函数里用var 你就可以得到一个全局变量) 写得代码简单,不涉及到大的项目或配合(写hello world是不会有什么 ...
- 虚拟机里面安装Openfiler 2.99
简介 Openfiler 由rPath Linux驱动,它是一个基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接存储 (NAS) 和基于块的存储区域网 (SAN).Open ...
- [BZOJ2959]长跑——新技能:LCT+缩圈
[BZOJ2959]长跑 试题描述 某校开展了同学们喜闻乐见的阳光长跑活动.为了能“为祖国健康工作五十年”,同学们纷纷离开寝室,离开教室,离开实验室,到操场参加3000米长跑运动.一时间操场上熙熙攘攘 ...
- c# 继承,多态,new /overrid 区别, 引用父类的方法
好久没碰c#了,偶尔需要制作点小工具.为了一个灵活的架构设计,需要对继承/多态有比较深刻的理解. 不料忘得差不多了,好吧,再来回忆下.直接上代码了,如下: using System; using Sy ...
- Java面向对象的继承
继承也是面向对象的又一重要特性,继承是类于类的一种关系,通俗来说狗属于动物类,那么狗这个类就继承了动物类 java中的继承是单继承的,一个类只能继承与一个父类 子类继承父类之后,子类就拥有了父类的所有 ...
- 1.python基础入门
作者:刘耀 出处:http://www.yaomr.com 欢迎转载 提示: 语法基于python3.5版本(会提示2.7版本和3.5版本的区别) Python命令行将以>>>开始, ...
- mybatis的insert简单使用