PHP Tree Depth Calendar Generation Maze and A * Automatic path finding algorithm example analysis, maze example Analysis _php tutorial

Source: Internet
Author: User

PHP tree Depth compilation of the Maze and a * Automatic path finding algorithm example analysis, Maze case analysis


In this paper, we present a maze and a * automatic pathfinding algorithm for the deep compilation calendar of Php tree. Share to everyone for your reference. The specific analysis is as follows:

A colleague recommended a think-through maze algorithm, see the feeling is good, turn into PHP
Think twice the maze algorithm is the use of tree depth traversal principle, so that the resulting 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

Don't say much nonsense, stick with the code

Maze Generation class:
Copy CodeThe code is as follows: 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 a Maze
Public Function Create () {
$this->_init ();
Return $this->_walk (rand (0, COUNT ($this->_grids)-1));
}
Get a 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 Pick Points
if ($rand) shuffle ($return);

if ($n = = 0) return $return;

if ($n = = 1) {
Return Array_pop ($return);
} else {
Return Array_slice ($return, 0, $n);
}
}
/**
|---------------------------------------------------------------
| A series of functions to create a 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 ($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 * path finding algorithm
Copy CodeThe code is as follows: 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;
}
Finding a way in a maze
Public Function Search ($start = False, $end = False) {
return $this->_search ($start, $end);
}
/**
|---------------------------------------------------------------
| Automatic path finding-A-star algorithm
|---------------------------------------------------------------
*/
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];
All selectable 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->_closed[$pNode] [' g '] + $this-&GT;_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-&GT;_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);
}
}

Full instance code click here to download this site.

There is a need you can directly under the demo, see the effect!

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/965566.html www.bkjia.com true http://www.bkjia.com/PHPjc/965566.html techarticle php Tree Depth compilation of the Maze and a * automatic pathfinding algorithm example analysis, Maze case analysis in this paper, we describe the depth of the PHP tree Generation Maze and a * automatic pathfinding algorithm. Share to the big ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.