http://poj.org/problem?id=3278

二维BFS

 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#define READ() freopen("in.txt", "r", stdin); using namespace std; struct Loc
{
int x, step;
Loc () {}
Loc(int x, int step) : x(x), step(step) {}
}; int N, K;
bool use[];
bool OK(int x)
{
if (x < || x > ) return false;
else return true;
}
int bfs()
{
queue<Loc> que;
que.push(Loc(N, ));
while (!que.empty())
{
Loc crt = que.front();
que.pop();
if(use[crt.x]) continue;
use[crt.x] = true;
int nx = crt.x - ;
if (OK(nx) && !use[nx])
{
if (nx == K) return crt.step+;
else que.push(Loc(nx, crt.step+));
}
nx = crt.x + ;
if (OK(nx))
{
if (nx == K && !use[nx]) return crt.step+;
else que.push(Loc(nx, crt.step+));
}
nx = *crt.x;
if (OK(nx) && !use[nx])
{
if (nx == K) return crt.step+;
else que.push(Loc(nx, crt.step+));
}
}
}
int main()
{
//READ()
scanf("%d%d", &N, &K);
if (N == K) //有N == K的坑点 严谨一点
{
cout << << endl;
return ;
}
memset(use, , sizeof(use));
int ans = bfs();
cout << ans << endl;
return ;
}

POJ 3248 Catch That Cow的更多相关文章

  1. BFS POJ 3278 Catch That Cow

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

  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(BFS,板子题)

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

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

  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(简单一维广搜)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 45648   Accepted: 14310 ...

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

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

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

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

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

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

随机推荐

  1. UISegmentedControl去掉背景色与UIScrollView联动

    UISegmentControl分段控制器是UIKit框架提供的一组按钮栏,提供多个可选的按钮,只能激活其中的一个,响应事件.主要用来在同一层次重要性下不同的信息展示或者不同的界面展示之间切换.例如手 ...

  2. MySQL 当记录不存在时insert,当记录存在时更新

    网上基本有三种解决方法. 第一种: 示例一:insert多条记录 假设有一个主键为 client_id 的 clients 表,可以使用下面的语句: INSERT INTO clients (clie ...

  3. java中同步(synchronized)详解

    一.开山篇: 1.synchronized的使用 一个程序中,如果该类中的代码可能运行于多线程环境下,那么就要考虑同步的问题.在Java中内置了语言级的同步原语--synchronized,这也大大简 ...

  4. SPICE-HTML5 鼠标指针BUG修复

    研究SPICE,找到了他们官方指定的HTML5客户端.下载下来用一下,发现跟网页VNC的水平差不多了.http://www.spice-space.org/page/Html5 服务端直接用QEMU起 ...

  5. ElasticSearch使用spring-data-elasticSearch的用法

    spring-data-Elasticsearch 使用之前,必须先确定版本,elasticsearch 对版本的要求比较高. spring和elasticsearch有两种链接方式,一种是用TCP协 ...

  6. 如何开发、本地测试、发布 Laravel 扩展包?

    如何开发.本地测试.发布 Laravel 扩展包?  Laravel/ 1年前/  4022 /  11   现在已经有了很多,关于如何开发 Laravel 扩展包的文章.但是大多文章写的太过片面,不 ...

  7. project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了

    project .mpp 查看当天工作任务 1.选择自己 2.选择起始和终止时间 就显示当天的任务了

  8. RNN静态与动态

    静态.多层RNN:import numpy as np import tensorflow as tf # 导入 MINST 数据集 from tensorflow.examples.tutorial ...

  9. C++静态全局变量和全局变量的区别

      静态全局变量 非静态全局变量 存储方式 静态存储 静态存储 作用域 定义该变量的源文件内 所有源文件 解释: 共同点:全局变量(外部变量)的说明之前再冠以static 就构 成了静态的全局变量.全 ...

  10. linux 复制文件夹内所有文件到另一个文件夹

    cp -Rf /home/user1/* /root/temp/将 /home/user1目录下的所有东西拷到/root/temp/下而不拷贝user1目录本身.即格式为:cp -Rf 原路径/ 目的 ...