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. Webform 内置对象2(Session、Application)、Repeater的Command操作

    内置对象: 1.Session:跟Cookies一样用来存储用户数据,但保存位置不同,保存在服务器内存上 每一台电脑访问服务器,都会是独立的一套session,key值都一样,但是内容都是不一样的 S ...

  2. 初试springWebMVC

    最近在尝试配置SpringMVC,发现各种坑. 首先遇到了这个问题. 'component-scan' and its parser class [org.springframework.contex ...

  3. 【译】OpenStack Heat基础介绍

    原文:http://blog.scottlowe.org/2014/05/01/an-introduction-to-openstack-heat/ 本文将简要地介绍OpenStack Heat. H ...

  4. 嵌入式ARM开发板学习方法步骤

    嵌入式开发就是指在嵌入式操作系统下进行开发,一般常用的系统有linux,android. 平台:Cortex-A9开发板 嵌入式技术学习如何入手,从何学起呢, 以下内容简单介绍嵌入式开发的学习步骤及如 ...

  5. jquery命名冲突

    nodeName是jquery的关键字

  6. iview modal 点击打开窗口,打开前先销毁里面的内容再打开

    <Modal v-model="addSubOrgModal" @on-cancel="addSubOrgCancel" @on-visible-chan ...

  7. json-server && axios

    json-server && axios vue2.0项目实战(3)使用axios发送请求 https://www.cnblogs.com/zhouyangla/p/6753673.h ...

  8. QT+创建两个不相干的窗口实现一个显示一个不显示

    因为两个窗口互不相干,所以需要重新创建一个窗口类subWidget subWidget.cpp文件 #ifndef SUBWIDGET_H #define SUBWIDGET_H #include & ...

  9. C ++ _基础之共用体

    由以下代码来进一步学习共用体 #include <stdio.h> #include<iostream> void main() { union un { int a; cha ...

  10. eclipse android SDK代理跟新

    启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...