如何使用PHP生成迷宫以及寻路求解?本文主要介绍了PHP生成迷宫及自动寻路算法,并对PHP生成迷宫及自动寻路算法详解。希望对大家有所帮助。

本文实例讲述了PHP树的深度编历生成迷宫及A*自动寻路算法。分享给大家供大家参考。具体分析如下:

有一同事推荐了三思的迷宫算法,看了感觉还不错,就转成php

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

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

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

废话不多说,贴上带代码

迷宫生成类: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 < count($this->_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*寻路算法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 < count($this->_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);

}

}

相关推荐:

python自动寻路算法_PHP生成迷宫及自动寻路算法详解相关推荐

  1. 实验三、prim算法生成迷宫,A*算法解迷宫(实验准备)

    目录 实验要求: 算法简介: prim算法: A*算法: 实验要求: 该项目的主要要求是:首先生成一个迷宫,要求随机生成.而生成迷宫有深度优先算法.prim算法.递归分割算法等.老师说建议使用prim ...

  2. python数据挖掘课程】二十一.朴素贝叶斯分类器详解及中文文本舆情分析

    #2018-04-06 13:52:30 April Friday the 14 week, the 096 day SZ SSMR python数据挖掘课程]二十一.朴素贝叶斯分类器详解及中文文本舆 ...

  3. python数据挖掘笔记】二十.KNN最近邻分类算法分析详解及平衡秤TXT数据集读取

    #2018-04-06 07:57:00 April Friday the 14 week, the 096 day SZ SSMR python数据挖掘笔记]二十.KNN最近邻分类算法分析详解及平衡 ...

  4. DL之DilatedConvolutions:Dilated Convolutions(膨胀卷积/扩张卷积)算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

    DL之DilatedConvolutions:Dilated Convolutions(膨胀卷积/扩张卷积)算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 Dilated Con ...

  5. DL之SqueezeNet:SqueezeNet算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

    DL之SqueezeNet:SqueezeNet算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 SqueezeNet算法的简介(论文介绍) 1.轻量级的CNN架构优势 2.主要特 ...

  6. DL之FCN:FCN算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

    DL之FCN:FCN算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 FCN算法的简介(论文介绍) 0.FCN性能-实验结果 1.全卷积神经网络的特点.局限性.缺点 FCN算法的架 ...

  7. DL之PanopticFPN:Panoptic FPN算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

    DL之PanopticFPN:Panoptic FPN算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 PanopticFPN算法的简介(论文介绍) 0.实验结果 1.不同架构比较 ...

  8. DL之PSPNet:PSPNet算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

    DL之PSPNet:PSPNet算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 PSPNet算法的简介(论文介绍) 0.实验结果 PSPNet算法的架构详解 PSPNet算法的案 ...

  9. DL之U-Net:U-Net算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略

    DL之U-Net:U-Net算法的简介(论文介绍).架构详解.案例应用等配图集合之详细攻略 目录 U-Net算法的简介(论文介绍) 0.实验结果 U-Net算法的架构详解 U-Net算法的案例应用 相 ...

最新文章

  1. git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改
  2. powershell 查看WMI信息和几个WMI类示例
  3. Fishe向量Fisher Vecotr(一)
  4. python实现WebsocketServer 服务端
  5. Nginx的Gzip和sendfile的共存问题
  6. 新手入门python的注意事项_【新手入门Python语言的方法】
  7. java 继承 子类 实例化_关于Java继承中父类和子类构造函数的问题
  8. 折半查找判定树及平均查找长度
  9. 【捣鼓】TypeError: “x” is not a constructor
  10. 【笔记】第2章 向量
  11. 牛客小白月赛1 F.三视图
  12. 走进大数据丨 一条让我虎躯一震的SQL
  13. 备份华为物理服务器目录到U盘
  14. 使用img标签能使用background-size:conver一样的效果
  15. 胖虎技术群Java后端的良师
  16. 即试即买丨帕克西首推假发自动售卖机,3D发型试戴变革假发购物体验!
  17. 维纳滤波器(Wiener Filter)在图像处理中的应用(一)
  18. 对于Tencent Files文件夹需要来自LAPTOP-PH9TIHGE\用户名的权限才能删除的解决办法
  19. 又一波网红餐饮店惨死,餐饮+新零售或将产生新格局
  20. 这些SCI写作万能句型,我都帮你总结好了!

热门文章

  1. 【随笔记】XR872 Codec 驱动移植和应用程序实例(附芯片调试方法)
  2. 有杀气童话服务器维护9月,《有杀气童话》9月24日例行维护通告
  3. 华为云-云耀云服务器之CentOS安装宝塔面板
  4. 【C++】命名空间namespace详解
  5. 国产手机纷纷研发自主操作系统,都可以丝滑兼容安卓应用
  6. Vue集成Bmap离线地图
  7. VC++的C运行库源代码位置
  8. Spring与Struts2的PK
  9. 【微信小程序】云开发 悟空工具
  10. etc init.d 没有mysql_【MySQL】centos6中/etc/init.d/下没有mysqld启动文件,怎么办