抓住那头牛(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)步。

 #include<iostream>
 #include<queue>
 #include<string>
 using namespace std;
 #define MAX 100000
 struct Step
 {
     int xx;//位置
     int steps;//到这一位置所需要走过的步数.
     Step(int aa,int bb):xx(aa),steps(bb){}
 };

 ];//

 queue<Step> opend;

 int main()
 {
     int N;
     int K;
     cout<<"please input N"<<endl;
     cin>>N;
     cout<<"please input K"<<endl;
     cin>>K;
     memset(flaged,,sizeof(flaged));
     opend.push(Step(N,));
     flaged[N]=;
     Step S=opend.front();
     while(!opend.empty())
     {
         S=opend.front();
         if(S.xx==K)
         {
             cout<<"find the caw ,we need "<<S.steps<<" steps"<<endl;
             system("pause");
             ;
         }

         >=&&flaged[S.xx-]!=)
         {
             opend.push(Step((S.xx-),(S.steps+)));
             flaged[S.xx-]=;
         }
         <=MAX&&flaged[S.xx+]!=)
         {
             opend.push(Step((S.xx+),(S.steps+)));
             flaged[S.xx+]=;
         }
         <=MAX&&flaged[(S.xx)*]!=)
         {
             opend.push(Step((S.xx*),(S.steps+)));
             flaged[S.xx*]=;
         }
         opend.pop();
     }
     system("pause");
     ;

 }

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

  1. POJ3984 BFS广搜--入门题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20816   Accepted: 12193 Descriptio ...

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

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description ...

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

    一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是 ...

  4. HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?

    这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others)  ...

  5. HDU 5652(二分+广搜)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/128683#problem/E 题目大意:给定一只含有0和1的地图,0代表可以走的格子,1代表不能走的格 子.之 ...

  6. nyoj 613 免费馅饼 广搜

    免费馅饼 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy ...

  7. poj 3278:Catch That Cow(简单一维广搜)

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

  8. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  9. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

随机推荐

  1. 引入了junit为什么还是用不了@Test注解

    pom文件明明引入了unit,为什么还是用不了@Test? 配置如下: <dependency> <groupId>junit</groupId> <arti ...

  2. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

  3. orcal中创建和删除表空间和用户

    1.创建表空间 create tablespace NW_DATA logging datafile 'F:\oracle\product\10.2.0\oradata\nwdb\NW_DATA.db ...

  4. Oracle旗下软件官网下载速度过慢解决办法

    平常下载Oracle旗下软件官网的产品资源,会发现速度很慢,如下载JDK和mysql时, 这样很浪费我们的时间 解决办法: 复制自己需要下载的资源链接 使用迅雷下载该资源 速度均很快 如下载Mysql ...

  5. AOP日志组件 多次获取post参数

    AOP日志组件 多次获取post参数 需求:新增接口日志组件.通过拦截器对接口URL进行拦截处理,然后将接口post请求的参数与结果,写入日志表. 问题:POST方法的参数是存储在request.ge ...

  6. touch-action css属性 滚动和缩放手势

    CSS 属性 touch-action 用于指定某个给定的区域是否允许用户操作,以及如何响应用户操作(比如浏览器自带的划动,缩放等) 默认情况下,平移(滚动) 和 缩放手势由浏览器专门处理.该属性用于 ...

  7. 【Java_基础】空串、空格串、null的区别

    1.表示的区别 string str1 = "";    //空串      str1.length() 等于 0 string str2 = " ";   / ...

  8. 小型LAMP搭建

    一.dns的搭建 安装dns服务 yum install bind 修改dns的主配置文件 [root@234c17 named]# vim /etc/named.conf // // named.c ...

  9. clipboard 在 vue 中的使用

    简介 页面中用 clipboard 可以进行复制粘贴,clipboard能将内容直接写入剪切板 安装 npm install --save clipboard 使用方法一 <template&g ...

  10. shell-code-1

    #!/bin/bash # online test tool: http://www.shucunwang.com/RunCode/shell/ name="pxy"#Attent ...