POJ 3287 (基础BFS) Catch That Cow
这是做的第一道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的更多相关文章
- bfs—Catch That Cow—poj3278
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 87152 Accepted: 27344 ...
- poj 3278 catch that cow BFS(基础水)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 61826 Accepted: 19329 ...
- BFS POJ 3278 Catch That Cow
题目传送门 /* BFS简单题:考虑x-1,x+1,x*2三种情况,bfs队列练练手 */ #include <cstdio> #include <iostream> #inc ...
- 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 Farmer John has been informed of the location of a fugitive cow and wants to catch he ...
- 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 ...
随机推荐
- HDOJ 1220 Cube
CubeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- PHP扩展迁移为兼容PHP7记录
PHP7扩展编写的时候,提供的一些内核方法和之前的PHP之前的版本并不能完全兼容.有不少方法参数做了调整.下面是在迁移过程中遇到的一些问题.记录下来,避免大家再踩坑. PHP7扩展开发之hello w ...
- NodeJS介绍
1.概述: Node.js是基于Chrome JavaScript运行时建立的一个平台,实际上它是对Google Chrome V8引擎进行了封装,它主要用于创建快速的.可扩展的网络应用.Node.j ...
- Solr笔记--转载
Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇分两部分的文章中,Lucene Java™ 的提交人 Grant Ingersoll ...
- http status 汇总
http status 汇总 常见HTTP状态码 200 OK 301 Moved Permanently 302 Found 304 Not Modified 307 Temporary Redir ...
- nefu 120 梅森素数
题意:给出p(1<p<=62),让你求Mp=2^p-1是否为梅森素数. 梅森素数:若p为素数,且Mp=2^p-1也是素数,则Mp为梅森素数.若p为合数,Mp=2^p-1一定为合数若p为素数 ...
- POJ 2181
#include <iostream> #include <cstdio> #include <cmath> #define MAXN 150005 #includ ...
- java基础知识回顾之抽象类
/* 抽象类: 抽象:笼统,模糊,看不懂!不具体. 特点: 1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中.该类必须也被abstract修饰. ...
- Linux :Can't start up: not enough memory
http://stackoverflow.com/questions/9634577/linux-cant-start-up-not-enough-memory
- Lock wait timeout exceeded; try restarting transaction
What gives this away is the word transaction. It is evident by the statement that the query was atte ...