PHP Tree Generation Maze and a * automatic path finding algorithm
Maze algorithm is the use of tree depth traversal principle, so that the maze is quite thin, and the number of dead end is relatively small!
There is only one path between any two points.
As for the A * pathfinding algorithm is the most popular one automatic path finding algorithm
The complete code has been uploaded, http://download.csdn.net/detail/hello_katty/8885779, here to do some simple explanation, but also need to think about their own hands. Don't say much nonsense, stick with code Maze generation class:
/** Create Maze class * @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 (); }//Set the Maze size public function set ($width = 6, $height = 6) {if ($width > 0) $this->_w = $width; if ($height > 0) $this->_h = $height; return $this; }//Take the Maze public function get () {return $this->_grids; }//Create Maze Public Function create () {$this->_init (); Return $this->_walk (rand (0, COUNT ($this->_grids)-1)); }//Get the Dead end point 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; }}//random fetch point if ($rand) shuffle ($return); if ($n = = 0) return $return; if ($n = = 1) {return array_pop ($return); } else {return array_slice ($return, 0, $n); }}/** the series function that generates the maze */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 ($thi S->_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_pu SH ($this->_grids, 0); }} return $this; }}
A * path finding algorithm
/** pathfinding Algorithm * @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; The public function set ($width, $height, $grids) {$this->_w = $width; $this->_h = $height; $this->_grids = $grids; return $this; }//Maze seek public function search ($start = False, $end = False) {return $this->_search ($start, $end); }/** Auto Pathfinding-A-star algorithm */Public function _search ($start = False, $end = False) {if ($start!== F alse) $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]; All optional directions into the queue 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-&G t;_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); }}
Extended Reading"Programming practical algorithm to achieve finishing" series of technical Articles to organize collection
Introduction to the 1 algorithm: the principle and implementation of selecting sort
2 a php bubble sorting algorithm pen questions
3WordPress User Password calculation algorithm
4PHP implementation of four commonly used sorting algorithms
5 very intuitive data structure and algorithm demonstration
6 in-depth discussion of various knapsack algorithm problems
7 Schematic insertion sorting algorithm
8 schematic heap sorting heap sort algorithm
9 C + + algorithm simulation for Joseph Ring (Josephus) problem
10 The problem of rabbit birth in fun algorithm
111 JavaScript implementations of some common algorithms
12 Fun algorithm for monkeys to eat peach problem
13 square root sqrt () function's underlying algorithm efficiency
Introduction to some related algorithms of 142-fork search tree
15 Euclidean algorithm (practiced hand)
16 Ant climbing wood pole problem algorithm idea
17 Some important algorithms in computer programming
18 Some common algorithmic problems in the interview
19JavaScript sorting algorithm of hill sort
Heap sequencing of 20JavaScript sorting algorithms
Merge sort of 21JavaScript sorting algorithm
Selection sort of 22JavaScript sorting algorithm
Fast sequencing of 23JavaScript sorting algorithms
Bubble sort of 24JavaScript sorting algorithm
Insertion sort of 25JavaScript sorting algorithm
26 Combined sorting algorithm explanation and example
27 Ugly numbers ugly number lookup algorithm
28 Algorithm for solving the factorial of large numbers
29 C + + implementation and performance testing of each sorting algorithm
30 a Monte-hall problem in the introduction of algorithms
31 implement a stack and get its smallest element
32 Firsthand experience the KMP algorithm
33 The fast thinking method of the interview algorithm problem
34 anti-clockwise rotation algorithm for matrices
35Hash Magic: Distributed hashing algorithm
36Hash Magic: Consistent hash algorithm
37 various implementation algorithms in reverse order of strings
38 Quick sort with recursive implementation
39 Collect some top software company classic algorithm face question
40 Fun algorithm: Monkey move banana problem
A review of the 411 mainstream personalized recommendation algorithms
42 Fun algorithm: Proportion of male and female students
43 Fun algorithm: Mouse test poison bottle problem
44 Data encryption and decryption using PHP's built-in DES algorithm functions
45 Nineth session: Relationship between data structure and algorithm
46 12th words: What kind of algorithm is a good algorithm
47 11th session: Five basic features of the algorithm
48 13th session: Performance analysis of the algorithm
49 10th words: What is an algorithm?
50 16th: Spatial complexity of the algorithm
51 15th session: Worst case and average of the algorithm
52 14th session: How to calculate the time complexity of the algorithm
53 stacking operations for sequential stacks
54 Sequential stacks: sequential storage structure of stacks
55 definition and approximate understanding of stacks
56 stack of abstract data types ADT
57 Chain Stack: The chain storage structure of the stack
58 stacking operations for chain stacks
59 getting the stack top element of the sequence stack
60 stacking operations for sequential stacks
61 Why use stacks for this data structure
62 The empty operation of chain stack and judging whether the link stack is empty
63 recursion, one of the important applications of the stack
How the 64 stack is implemented recursively
65 the stack operation of the chain stack
66 initialization and traversal of chain stacks
67 Random Talk recursion: the thought of recursion
68 Random Talk recursion: Two conditions to be met for recursion
69 Random Talk recursion: recursive judgement of the phenomenon of string palindrome
70 Random Talk recursion: Recursive implementation of binary search algorithm
71 Random Talk recursion: the efficiency problem of recursion
72 Random Talk recursion: recursion and circulation
73 Ramble about recursion: Is it a matter of loops and iterations?
74 Random Talk recursion: Starting with Fibonacci to understand tail recursion
75 rambling recursion: tail recursion and CPS
76 Random Talk recursion: supplement some continuation knowledge
77 Random Talk recursion: tail recursion and its optimization in PHP
78 Random Talk recursion: The optimization of tail recursion from the assembly perspective
79 Quick sort of learning: starting with guessing numbers
80 Quick sort of learning: Another look at the problem of calling the ball
81 Learning in the quick sort: information entropy
82 Quick sort of learning: The process of fast sequencing
83 Quick sort of learning: Hall and Quick sort
84 Learning in the quick sort: The realization of the hall quick-line
85 Learning in fast sequencing: Hub meta-selection and algorithmic efficiency
86 Quick sort of learning: randomize fast rows
87Java programmers must master the 8 sorting algorithm
88RSA,DSA and other encryption and decryption algorithm introduction
89 Permissions Project Summary (i) Authority design
Extended Reading
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PHP Tree Generation Maze and a * automatic path finding algorithm