这是做的第一道BFS,很基础很简单的题目

广度优先搜索算法如下:(用QUEUE)
(1) 把初始节点S0放入Open表中;
(2) 如果Open表为空,则问题无解,失败
退出;
(3) 把Open表的第一个节点取出放入
Closed表,并记该节点为n;
(4) 考察节点n是否为目标节点。若是,
则得到问题的解,成功退出;
(5) 若节点n不可扩展,则转第(2)步;
(6) 扩展节点n,将其不在Closed表和
Open表中的子节点(判重)放入Open表的尾部
,并为每一个子节点设置指向父节点的指针(
或记录节点的层次),然后转第(2)步。

一层一层的往深了的搜索,直到遇到所求解。那么深度就是最短步数,还有要注意判重,其中visited数组就起到了判重的作用。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int n, k;
const int maxn = ;
int visited[maxn + ]; //判重标记
struct Step
{
int x;
int steps;
Step(int xx, int s):x(xx), steps(s){}
};
queue<Step> q;
int main(void)
{
scanf("%d%d", &n, &k);
memset(visited, , sizeof(visited));
q.push(Step(n, ));
visited[n] = ;
while(!q.empty())
{
Step s = q.front();
if(s.x == k)
{//找到目标
printf("%d\n", s.steps);
return ;
}
else
{
if(s.x - >= && !visited[s.x-])
{
q.push(Step(s.x-, s.steps+));
visited[s.x-] = ;
}
if(s.x + <= maxn && !visited[s.x+])
{
q.push(Step(s.x+, s.steps+));
visited[s.x+] = ;
}
if(s.x* <= maxn && !visited[s.x*])
{
q.push(Step(s.x*, s.steps+));
visited[s.x*] = ;
}
q.pop();
}
}
return ;
}

代码君

POJ 3287 (基础BFS) Catch That Cow的更多相关文章

  1. bfs—Catch That Cow—poj3278

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 87152   Accepted: 27344 ...

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

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

  3. BFS POJ 3278 Catch That Cow

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

  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 Farmer John has been informed of the location of a fugitive cow and wants to catch he ...

  9. poj 3278(hdu 2717) Catch That Cow(bfs)

    Catch That Cow Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service

    准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...

  2. c3p0 --1

    # # This file is detritus from various testing attempts  # the values below may change, and often do ...

  3. Subclasses

    Given a collection of numbers, return all possible subclasses. public class Solution { public List&l ...

  4. 转载 Memcached BinaryProtocol incr指令内存泄露的bug

    缘起 最近有个分布式限速的需求.支付宝的接口双11只允许每秒调用10次. 单机的限速,自然是用google guava的RateLimiter. http://docs.guava-libraries ...

  5. acdream1116 Gao the string!(扩展KMP)

    今天是字符串填坑的一天,首先填的第一个坑是扩展KMP.总结一下KMP和扩展KMP的区别. 在这里s是主串,t是模式串. KMP可以求出的是以s[i]为结尾的串和 t前缀匹配的最长的长度.假如这个长度是 ...

  6. LA 4287

    Consider the following exercise, found in a generic linear algebra textbook. Let A be an n × n matri ...

  7. POJ 2151 Check the difficulty of problems (概率dp)

    题意:给出m.t.n,接着给出t行m列,表示第i个队伍解决第j题的概率. 现在让你求:每个队伍都至少解出1题,且解出题目最多的队伍至少要解出n道题的概率是多少? 思路:求补集. 即所有队伍都解出题目的 ...

  8. 即时通讯UI-聊天界面(UITableView显示左右两人聊天)

    目录 1.创建UITableView对象并设置相关属性 2.创建cellModel模型 //枚举类型 typedef enum { ChatMessageFrom = ,//来自对方的消息 ChatM ...

  9. SQL技术内幕-6 rank()over(order by XX COLLATE) 的用法

    DECLARE @Names TABLE ( name VARCHAR(20) ); INSERT INTO @Names VALUES ('DeSzmetch'),('DESZMETCH'),('D ...

  10. 使用时间戳引入css、js文件

    前言 最近在一家创业公司实习,主要负责新版官网和商家平台管理系统的前端开发和维护,每次测试都要上传文件到ftp服务器端测试,初期由于更新修改比较频繁,每次都是直接上传覆盖css.js.php文件,链接 ...