CatchTheCaw ----广搜入门

抓住那头牛(POJ3278)
农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上
,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)
。农夫有两种移动方式:
1、从X移动到X-1或X+1,每次移动花费一分钟
2、从X移动到2*X,每次移动花费一分钟
假设牛没有意识到农夫的行动,站在原地不动。农夫最少要
花多少时间才能抓住牛?

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

 1 #include<iostream>
 2 #include<queue>
 3 #include<string>
 4 using namespace std;
 5 #define MAX 100000
 6 struct Step
 7 {
 8     int xx;//位置
 9     int steps;//到这一位置所需要走过的步数.
10     Step(int aa,int bb):xx(aa),steps(bb){}
11 };
12
13 int flaged[MAX+10];//
14
15 queue<Step> opend;
16
17 int main()
18 {
19     int N;
20     int K;
21     cout<<"please input N"<<endl;
22     cin>>N;
23     cout<<"please input K"<<endl;
24     cin>>K;
25     memset(flaged,0,sizeof(flaged));
26     opend.push(Step(N,0));
27     flaged[N]=1;
28     Step S=opend.front();
29     while(!opend.empty())
30     {
31         S=opend.front();
32         if(S.xx==K)
33         {
34             cout<<"find the caw ,we need "<<S.steps<<" steps"<<endl;
35             system("pause");
36             return 0;
37         }
38
39         if(S.xx-1>=0&&flaged[S.xx-1]!=1)
40         {
41             opend.push(Step((S.xx-1),(S.steps+1)));
42             flaged[S.xx-1]=0;
43         }
44         if(S.xx+1<=MAX&&flaged[S.xx+1]!=1)
45         {
46             opend.push(Step((S.xx+1),(S.steps+1)));
47             flaged[S.xx+1]=0;
48         }
49         if((S.xx)*2<=MAX&&flaged[(S.xx)*2]!=1)
50         {
51             opend.push(Step((S.xx*2),(S.steps+1)));
52             flaged[S.xx*2]=0;
53         }
54         opend.pop();
55     }
56     system("pause");
57     return 0;
58
59 }
时间: 2024-08-24 11:26:52

CatchTheCaw ----广搜入门的相关文章

poj 3984:迷宫问题(广搜,入门题)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

第一次广搜!HDU1548--A Strange Lift

一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是最基本的广搜了 只是一个二叉树 所以先画个二叉树出来看一下广搜的顺序 每一个节点下一层的节点入队之后就把这个节点出队 对每一个节点 bfs的顺序是这样的 1.将这个节点的左节点入队(要判断左节点是否已经入队以及是否合适) 2.将这个节点的右节点入队(同样也要盘算右节点是否已经入队以及是否满足条件)

NYOJ 284 坦克大战 &amp;&amp; POJ 2312 Battle City (广搜+优先队列)

链接:click here~~ 题意: 描述 Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty space

Catch That Cow(广搜)

个人心得:其实有关搜素或者地图啥的都可以用广搜,但要注意标志物不然会变得很复杂,想这题,忘记了标志,结果内存超时: 将每个动作扔入队列,但要注意如何更简便,更节省时间,空间 Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and t

codevs 1225:八数码难题【双向广搜】

这里是传送门 这道题用普通BFS是可以做的,但是很明显没得过,效率太低了.效率更高的算法A*和双向广搜都可取,这写一下双向广搜的. 注意题目中的判重很重要,可以转化成九位数用hash来解决这个问题. #include <set> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define

迷宫广搜

上学期学了C,这学期学C++.感觉最难的还是算法,上周作业的一道广搜题是我第一次接触广搜,由于第一学期刚学编程就接触的太多的算法难题,不禁对代码产生畏惧,不过还好没有放弃,虽然算法很难,但我慢慢找到了一点学数学时的乐趣.先介绍一下这道未来的我看过来会觉得很简单一道题吧 You are provided a maze(迷宫), and you need to program to find the least steps to walk from the start to the end.And

宽搜和广搜、

广搜与深搜的小区别 一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(好比3步就必需达到前提)"的标题,它的空间效率高,然则找到的不必定是最优解,必需记实并完成全数搜索,故一般情况下,深搜需要很是高效的剪枝(优化). 像搜索最短路径这些的很显著若是用广搜,因为广搜的特征就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的操作,像什么起码状态转换也是可以操作的.深搜就

hdu1241详解 Java广搜搞定

import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int m = sc.nextInt();//输入地图的行数 int n = sc.nextInt();//输入地图的列数 if (m == 0) {//若m=0则退出程序 break; } // 初始化图

poj 1166 The Clocks 记录路径的广搜

题意: 给9个时钟的初始状态,和一些对某几个钟的操作,求最少经过几步能到目标状态(全指向12点). 分析: 明显的广搜,但实现起来的细节要注意:1.因为要记录路径,所以要在整个程序执行过程中扩展出的节点在输出路径前不能销毁, 故采用静态内存分配的方法(开node[600000],用get_node()创建节点.2.queue<node>比queue<int>要多花1别的时间. //poj 1166 //sep9 #include <iostream> #include