热门标签 | HotTags
当前位置:  开发笔记 > 人工智能 > 正文

谈谈PHP树生成迷宫及A*自动寻路算法

迷宫算法是采用树的深度遍历原理,这样生成的迷宫相当的细,而且死胡同数量相对较少!任意两点之间都存在唯一的一条通路。

迷宫算法是采用树的深度遍历原理,这样生成的迷宫相当的细,而且死胡同数量相对较少!

任意两点之间都存在唯一的一条通路。

至于A*寻路算法是最大众化的一全自动寻路算法

完整代码已上传, http://download.csdn.net/detail/hello_katty/8885779 ,此处做些简单解释,还需要大家自己思考动手。废话不多说,贴上带代码

迷宫生成类:


/** 生成迷宫类
 * @date 2015-07-10
 * @edit http://www.lai18.com
 * @version 1
 **/
class Maze{
    // Maze Create
    private $_w;
    private $_h;
    private $_grids;
    private $_walkHistory;
    private $_walkHistory2;
    private $_targetSteps;
    // Construct
    public function Maze() {
  $this->_w = 6;
  $this->_h = 6;
  $this->_grids = array();
    }
    // 设置迷宫大小
    public function set($width = 6, $height = 6) {
  if ( $width > 0 ) $this->_w = $width;
  if ( $height > 0 ) $this->_h = $height;
  return $this;
    }
    // 取到迷宫
    public function get() {
  return $this->_grids;
    }
    // 生成迷宫
    public function create() {
  $this->_init();
  return $this->_walk(rand(0, count($this->_grids) -1 ));
    }
    // 获取死胡同点
    public function block($n = 0, $rand = false) {
  $l = count($this->_grids);
  for( $i = 1; $i <$l; $i++ ) {
      $v = $this->_grids[$i];
      if ( $v == 1 || $v == 2 || $v == 4 || $v == 8 ) {
    $return[] = $i;
      }
  }
  // 随机取点
  if ( $rand ) shuffle($return);
  if ( $n == 0 ) return $return;
  if ( $n == 1 ) {
      return array_pop($return);
  } else {
      return array_slice($return, 0, $n);
  }
    }
    /**
    生成迷宫的系列函数
    */
    private function _walk($startPos) {
  $this->_walkHistory = array();
  $this->_walkHistory2 = array();
  $curPos = $startPos;
  while ($this->_getNext0() != -1) {
      $curPos = $this->_step($curPos);
      if ( $curPos === false ) break;
  }
  return $this;
    }
    private function _getTargetSteps($curPos) {
  $p = 0;
  $a = array();
  $p = $curPos - $this->_w;
  if ($p > 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
      array_push($a, $p);
  } else {
      array_push($a, -1);
  }
  $p = $curPos + 1;
  if ($p % $this->_w != 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
      array_push($a, $p);
  } else {
      array_push($a, -1);
  }
  $p = $curPos + $this->_w;
  if ($p _grids) && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
      array_push($a, $p);
  } else {
      array_push($a, -1);
  }
  $p = $curPos - 1;
  if (($curPos % $this->_w) != 0 && $this->_grids[$p] === 0 && ! $this->_isRepeating($p)) {
      array_push($a, $p);
  } else {
      array_push($a, -1);
  }
  return $a;
    }
    private function _noStep() {
  $l = count($this->_targetSteps);
  for ($i = 0; $i <$l; $i ++) {
      if ($this->_targetSteps[$i] != -1) return false;
  }
  return true;
    }
    private function _step($curPos) {
  $this->_targetSteps = $this->_getTargetSteps($curPos);
  if ( $this->_noStep() ) {
      if ( count($this->_walkHistory) > 0 ) {
    $tmp = array_pop($this->_walkHistory);
      } else {
    return false;
      }
      array_push($this->_walkHistory2, $tmp);
      return $this->_step($tmp);
  }
  $r = rand(0, 3);
  while ( $this->_targetSteps[$r] == -1) {
      $r = rand(0, 3);
  }
  $nextPos = $this->_targetSteps[$r];
  $isCross = false;
  if ( $this->_grids[$nextPos] != 0)
      $isCross = true;
  if ($r == 0) {
      $this->_grids[$curPos] ^= 1;
      $this->_grids[$nextPos] ^= 4;
  } elseif ($r == 1) {
      $this->_grids[$curPos] ^= 2;
      $this->_grids[$nextPos] ^= 8;
  } elseif ($r == 2) {
      $this->_grids[$curPos] ^= 4;
      $this->_grids[$nextPos] ^= 1;
  } elseif ($r == 3) {
      $this->_grids[$curPos] ^= 8;
      $this->_grids[$nextPos] ^= 2;
  }
  array_push($this->_walkHistory, $curPos);
  return $isCross ? false : $nextPos;
    }
    private function _isRepeating($p) {
  $l = count($this->_walkHistory);
  for ($i = 0; $i <$l; $i ++) {
      if ($this->_walkHistory[$i] == $p) return true;
  }
  $l = count($this->_walkHistory2);
  for ($i = 0; $i <$l; $i ++) {
      if ($this->_walkHistory2[$i] == $p) return true;
  }
  return false;
    }
    private function _getNext0() {
  $l = count($this->_grids);
  for ($i = 0; $i <= $l; $i++ ) {
      if ( $this->_grids[$i] == 0) return $i;
  }
  return -1;
    }
    private function _init() {
  $this->_grids = array();
  for ($y = 0; $y <$this->_h; $y ++) {
      for ($x = 0; $x <$this->_w; $x ++) {
    array_push($this->_grids, 0);
      }
  }
  return $this;
    }
}
A*寻路算法

/** 寻路算法
 * @date 2015-07-10
 * @edit http://www.lai18.com
 * @version 1
 **/
class AStar{
    // A-star
    private $_open;
    private $_closed;
    private $_start;
    private $_end;
    private $_grids;
    private $_w;
    private $_h;
    // Construct
    public function AStar(){
  $this->_w = null;
  $this->_h = null;
  $this->_grids = null;
    }
    public function set($width, $height, $grids) {
  $this->_w = $width;
  $this->_h = $height;
  $this->_grids = $grids;
  return $this;
    }
    // 迷宫中寻路
    public function search($start = false, $end = false) {
  return $this->_search($start, $end);
    }
    /**
    自动寻路 - A-star 算法
       */
    public function _search($start = false, $end = false) {
  if ( $start !== false ) $this->_start = $start;
  if ( $end !== false ) $this->_end = $end;
  $_sh = $this->_getH($start);
  $point['i'] = $start;
  $point['f'] = $_sh;
  $point['g'] = 0;
  $point['h'] = $_sh;
  $point['p'] = null;
  $this->_open[] = $point;
  $this->_closed[$start] = $point;
  while ( 0 _open) ) {
      $minf = false;
      foreach( $this->_open as $key => $maxNode ) {
    if ( $minf === false || $minf > $maxNode['f'] ) {
        $minIndex = $key;
    }
      }
      $nowNode = $this->_open[$minIndex];
      unset($this->_open[$minIndex]);
      if ( $nowNode['i'] == $this->_end ) {
    $tp = array();
    while( $nowNode['p'] !== null ) {
        array_unshift($tp, $nowNode['p']);
        $nowNode = $this->_closed[$nowNode['p']];
    }
    array_push($tp, $this->_end);
    break;
      }
      $this->_setPoint($nowNode['i']);
  }
  $this->_closed = array();
  $this->_open = array();
  return $tp;
    }
    private function _setPoint($me) {
  $point = $this->_grids[$me];
  // 所有可选方向入队列
  if ( $point & 1 ) {
      $next = $me - $this->_w;
      $this->_checkPoint($me, $next);
  }
  if ( $point & 2 ) {
      $next = $me + 1;
      $this->_checkPoint($me, $next);
  }
  if ( $point & 4 ) {
      $next = $me + $this->_w;
      $this->_checkPoint($me, $next);
  }
  if ( $point & 8 ) {
      $next = $me - 1;
      $this->_checkPoint($me, $next);
  }
    }
    private function _checkPoint($pNode, $next) {
  if ( $this->_closed[$next] ) {
      $_g = $this->_closed[$pNode]['g'] + $this->_getG($next);
      if ( $_g <$check['g'] ) {
    $this->_closed[$next]['g'] = $_g;
    $this->_closed[$next]['f'] = $this->_closed[$next]['g'] + $this->_closed[$next]['h'];
    $this->_closed[$next]['p'] = $pNode;
      }
  } else {
      $point['p'] = $pNode;
      $point['h'] = $this->_getH($next);
      $point['g'] = $this->_getG($next);
      $point['f'] = $point['h'] + $point['g'];
      $point['i'] = $next;
      $this->_open[] = $point;
      $this->_closed[$next] = $point;
  }
    }
    private function _getG($point) {
  return abs($this->_start - $point);
    }
    private function _getH($point) {
  return abs($this->_end - $point);
    }
}


推荐阅读
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • qt学习(六)数据库注册用户的实现方法
    本文介绍了在qt学习中实现数据库注册用户的方法,包括登录按钮按下后出现注册页面、账号可用性判断、密码格式判断、邮箱格式判断等步骤。具体实现过程包括UI设计、数据库的创建和各个模块调用数据内容。 ... [详细]
  • “你永远都不知道明天和‘公司的意外’哪个先来。”疫情期间,这是我们最战战兢兢的心情。但是显然,有些人体会不了。这份行业数据,让笔者“柠檬” ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • [译]技术公司十年经验的职场生涯回顾
    本文是一位在技术公司工作十年的职场人士对自己职业生涯的总结回顾。她的职业规划与众不同,令人深思又有趣。其中涉及到的内容有机器学习、创新创业以及引用了女性主义者在TED演讲中的部分讲义。文章表达了对职业生涯的愿望和希望,认为人类有能力不断改善自己。 ... [详细]
  • 无线认证设置故障排除方法及注意事项
    本文介绍了解决无线认证设置故障的方法和注意事项,包括检查无线路由器工作状态、关闭手机休眠状态下的网络设置、重启路由器、更改认证类型、恢复出厂设置和手机网络设置等。通过这些方法,可以解决无线认证设置可能出现的问题,确保无线网络正常连接和上网。同时,还提供了一些注意事项,以便用户在进行无线认证设置时能够正确操作。 ... [详细]
  • 本文介绍了游戏开发中的人工智能技术,包括定性行为和非定性行为的分类。定性行为是指特定且可预测的行为,而非定性行为则具有一定程度的不确定性。其中,追逐算法是定性行为的具体实例。 ... [详细]
  • JavaScript设计模式之策略模式(Strategy Pattern)的优势及应用
    本文介绍了JavaScript设计模式之策略模式(Strategy Pattern)的定义和优势,策略模式可以避免代码中的多重判断条件,体现了开放-封闭原则。同时,策略模式的应用可以使系统的算法重复利用,避免复制粘贴。然而,策略模式也会增加策略类的数量,违反最少知识原则,需要了解各种策略类才能更好地应用于业务中。本文还以员工年终奖的计算为例,说明了策略模式的应用场景和实现方式。 ... [详细]
  • 本文介绍了PhysioNet网站提供的生理信号处理工具箱WFDB Toolbox for Matlab的安装和使用方法。通过下载并添加到Matlab路径中或直接在Matlab中输入相关内容,即可完成安装。该工具箱提供了一系列函数,可以方便地处理生理信号数据。详细的安装和使用方法可以参考本文内容。 ... [详细]
  • 本文详细介绍了相机防抖的设置方法和使用技巧,包括索尼防抖设置、VR和Stabilizer档位的选择、机身菜单设置等。同时解释了相机防抖的原理,包括电子防抖和光学防抖的区别,以及它们对画质细节的影响。此外,还提到了一些运动相机的防抖方法,如大疆的Osmo Action的Rock Steady技术。通过本文,你将更好地理解相机防抖的重要性和使用技巧,提高拍摄体验。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
author-avatar
我爱看电视OK
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有