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

时间:2022-08-28 18:19:36

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 < 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*寻路算法

/** 寻路算法
* @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 < 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); } }

延伸阅读

1算法导论:选择排序的原理与实现

2一道PHP冒泡排序算法笔试题

3WordPress的用户password计算算法

4PHP实现四种经常使用的排序算法

5很直观的数据结构与算法演示

6深入探讨各种背包算法问题

7图解插入排序算法

8图解堆排序Heap Sort算法

9约瑟夫环(Josephus)问题的C++算法模拟

10趣味算法之兔子产子问题

11一些常见算法的JavaScript实现

12趣味算法之猴子吃桃问题

13平方根sqrt()函数的底层算法效率

14二叉搜索树的一些相关算法介绍

15欧几里德算法(辗转相处法)练手

16蚂蚁爬木杆问题的算法思路

17计算机编程中一些重要的算法

18面试中常见的一些算法问题

19JavaScript排序算法之希尔排序

20JavaScript排序算法之堆排序

21JavaScript排序算法之归并排序

22JavaScript排序算法之选择排序

23JavaScript排序算法之高速排序

24JavaScript排序算法之冒泡排序

25JavaScript排序算法之插入排序

26合并排序算法讲解与样例

27丑数Ugly Number查找算法

28求大数阶乘的算法

29各排序算法的C++实现与性能測试

30算法导论中一个蒙提霍尔问题

31实现一个栈并获取其最小元素

32亲身体验一下KMP算法

33面试算法题的高速思考方法

34矩阵逆时针旋转的算法

35Hash魔法:分布式哈希算法

36Hash魔法:一致性 hash 算法

37字符串逆序的各种实现算法

38用递归实现的高速排序

39收集一些top软件公司经典算法面试题

40趣味算法:猴子搬香蕉问题

41一些主流的个性化推荐算法评析

42趣味算法:生男生女的比例

43趣味算法:老鼠试毒瓶问题

44使用PHP内置的DES算法函数实现数据加密解密

45第九话:数据结构与算法的关系

46第12话:什么样的算法才是好算法

47第11话:算法的五个基本特征

48第13话:算法的性能分析

49第10话:什么是算法?

50第16话:算法的空间复杂度

51第15话:算法的最坏情况与平均情况

52第14话:怎样计算算法的时间复杂度

53顺序栈的进栈操作

54顺序栈:栈的顺序存储结构

55栈的定义与大概理解

56栈的抽象数据类型ADT

57链栈:栈的链式存储结构

58链栈的进栈操作

59获取顺序栈的栈顶元素

60顺序栈的出栈操作

61为什么要使用栈这样的数据结构

62链栈的置空操作与推断链栈是否为空

63递归。栈的重要应用之中的一个

64栈是怎样实现递归的

65链栈的出栈操作

66链栈的初始化与遍历

67漫谈递归:递归的思想

68漫谈递归:递归须要满足的两个条件

69漫谈递归:字符串回文现象的递归推断

70漫谈递归:二分查找算法的递归实现

71漫谈递归:递归的效率问题

72漫谈递归:递归与循环

73漫谈递归:循环与迭代是一回事吗?

74漫谈递归:从斐波那契開始了解尾递归

75漫谈递归:尾递归与CPS

76漫谈递归:补充一些Continuation的知识

77漫谈递归:PHP里的尾递归及其优化

78漫谈递归:从汇编看尾递归的优化

79高速排序里的学问:从猜数字開始

80高速排序里的学问:再看看称球问题

81高速排序里的学问:信息熵

82高速排序里的学问:高速排序的过程

83高速排序里的学问:霍尔与高速排序

84高速排序里的学问:霍尔快排的实现

85高速排序里的学问:枢纽元选择与算法效率

86高速排序里的学问:随机化快排

87Java程序猿必须掌握的8大排序算法

88RSA,DSA等加解密算法介绍

89权限项目总结(一)权限设计

延伸阅读


PHP树生成迷宫及A*自己主动寻路算法的更多相关文章

  1. BZOJ4006 JLOI2015 管道连接&lpar;斯坦纳树生成森林&rpar;

    4006: [JLOI2015]管道连接 Time Limit: 30 Sec Memory Limit: 128 MB Description 小铭铭最近进入了某情报部门,该部门正在被如何建立安全的 ...

  2. 玩转Web之easyui&lpar;二&rpar;-----easy ui 异步加载生成树节点(Tree),点击树生成tab&lpar;选项卡&rpar;

    关于easy ui 异步加载生成树及点击树生成选项卡,这里直接给出代码,重点部分代码中均有注释 前台: $('#tree').tree({ url: '../servlet/School_Tree?i ...

  3. canvas——随机生成迷宫

    先上图. 效果 代码 随机生成迷宫要求任意两点都能够找到相同的路径,也就是说,迷宫是一个连通图.随机生成迷宫可以使用普里姆算法.广度优先算法.深度优先算法等实现.这里将使用普里姆算法通过生成最小数的方 ...

  4. php生成迷宫和迷宫寻址算法实例

    较之前的终于有所改善.生成迷宫的算法和寻址算法其实是一样.只是一个用了遍历一个用了递归.参考了网上的Mike Gold的算法. <?php //zairwolf z@cot8.com heade ...

  5. UWP开发:自动生成迷宫&amp&semi;自动寻路算法(3)

    + , + ];//0<=x<=12 0<=y<=24 private static Random Rd = new Random(); 首先声明mazeMap存储数据,声明了 ...

  6. Prim算法生成迷宫

    初始化地图 function initMaze(r,c){ let row = new Array(2 * r + 1) for(let i = 0; i < row.length; i++){ ...

  7. Unity&lowbar;Dungeonize 随机生成迷宫

    本文对随机生成迷宫的实现思路进行记录,其作用在于为游戏过程提供随机性以及节省开发周期,下面是Dungeonize的结构 随机迷宫的生成主要包括几个阶段 1.生成房间体结构,为墙体,自定义房间,自定义物 ...

  8. &period;NET技术-6&period;0&period; Expression 表达式树 生成 Lambda

    .NET技术-6.0. Expression 表达式树 生成 Lambda public static event Func<Student, bool> myevent; public ...

  9. 【Javascript &plus; Vue】实现随机生成迷宫图片

    前言 成品预览:https://codesandbox.io/s/maze-vite-15-i7oik?file=/src/maze.js 不久前写了一篇文章介绍了如何解迷宫:https://www. ...

随机推荐

  1. js正则表达式校验非负浮点数:&Hat;&lbrack;1-9&rsqb;&bsol;d&ast;&bsol;&period;&bsol;d&ast;&vert;0&bsol;&period;&bsol;d&ast;&lbrack;1-9&rsqb;&bsol;d&ast;&vert;0&quest;&bsol;&period;0&plus;&vert;0&dollar;

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. js&lowbar;继承

    一,js中对象继承 js中有三种继承方式 1.js原型(prototype)实现继承 复制代码代码如下: <SPAN style="<SPAN style="FONT- ...

  3. Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...

  4. HR外包系统 - 客户公司薪资规则 报表需求 记入系统

    1 薪酬规则,包括 常用薪资项目 2 报表需求,特别是报表排序规则 3 特殊项说明记录 另外包括客户公司监控的日期设置

  5. GPS accuracy in Android

    Get the estimated accuracy of this location, in meters. We define accuracy as the radius of 68% conf ...

  6. BZOJ3994&colon; &lbrack;SDOI2015&rsqb;约数个数和

    Description  设d(x)为x的约数个数,给定N.M,求     Input 输入文件包含多组测试数据. 第一行,一个整数T,表示测试数据的组数. 接下来的T行,每行两个整数N.M.   O ...

  7. AndrdoidStudio 2个jar包引起的异常Duplicate files copied in APK META-INF&sol;LICENSE&period;txt

    在build.gradle中与compileSdkVersion **.buildToolsVersion “**.**.*"或defaultConfig 同级添加如下代码 packagin ...

  8. IO - FileUtils

    Apache Commons IO好用的功能主要集中在工具类FileUtil中,包含了建立,删除,复制,移动,比较文件新旧,递归枚举目录清空目录,一次读取整个文件等.以下是一个我认为有用的列表: 1. ...

  9. 互联网常见Open API文档资源

    原文地址:http://blog.sina.com.cn/s/blog_4d8713560100y272.html 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网 ...

  10. hdu 2642 二维树状数组 单点更新区间查询 模板水题

    Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Subm ...