PHP tree deep calendar generation maze and A * Automatic route finding algorithm instance Analysis _ PHP Tutorial

Source: Internet
Author: User
PHP tree deep calendar generation maze and A * Automatic route searching algorithm instance analysis. PHP tree deep calendar generation maze and A * Automatic path searching algorithm example analysis this article mainly introduces the PHP tree deep calendar generation maze and A * Automatic path searching algorithm, an example shows how to use php to create A maze for deep calendar generation of A * PHP tree and an example of A * automatic pathfinding algorithm.

This article mainly introduces the deep calendar generation maze of the PHP tree and the * automatic pathfinding algorithm. The example shows how to use php to implement the * pathfinding algorithm, which has some reference value, for more information, see

The example in this article describes the deep calendar generation maze of the PHP tree and the * automatic pathfinding algorithm. Share it with you for your reference. The specific analysis is as follows:

One of my colleagues recommended the three-thought-provoking maze algorithm. after reading the algorithm, it turned into php.
Think twice of the maze algorithm is based on the deep traversal principle of the tree, so that the generated maze is quite fine, and the number of dead ends is relatively small!
There is a unique path between any two points.

As for A * pathfinding algorithm, it is the most popular automatic pathfinding algorithm.

Not much nonsense. paste the code

Maze generation class:

The code is as follows:

Class Maze {
// Maze Create
Private $ _ w;
Private $ _ h;
Private $ _ grids;
Private $ _ History history;
Private $ _ export History2;
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;
}
// Obtain the maze
Public function get (){
Return $ this-> _ grids;
}
// Generate a Maze
Public function create (){
$ This-> _ init ();
Return $ this-> _ walk (rand (0, count ($ this-> _ grids)-1 ));
}
// Obtain dead end points
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 vertex fetch
If ($ rand) shuffle ($ return );

If ($ n = 0) return $ return;

If ($ n = 1 ){
Return array_pop ($ return );
} Else {
Return array_slice ($ return, 0, $ n );
}
}
/**
| ---------------------------------------------------------------
| Generation of functions in the maze
| ---------------------------------------------------------------
*/
Private function _ walk ($ startPos ){
$ This-> _ History history = array ();
$ This-> _ required History2 = 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 $;
}
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-> _ History)> 0 ){
$ Tmp = array_pop ($ this-> _ History history );
} Else {
Return false;
}
Array_push ($ this-> _ export History2, $ 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-> _ History, $ curPos );
Return $ isCross? False: $ nextPos;
}
Private function _ isRepeating ($ p ){
$ L = count ($ this-> _ History history );
For ($ I = 0; $ I <$ l; $ I ++ ){
If ($ this-> _ History [$ I] = $ p) return true;
}
$ L = count ($ this-> _ your History2 );
For ($ I = 0; $ I <$ l; $ I ++ ){
If ($ this-> _ your History2 [$ 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 * pathfinding algorithm

The 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;
}
// Seek path in the maze
Public function search ($ start = false, $ end = false ){
Return $ this-> _ search ($ start, $ end );
}
/**
| ---------------------------------------------------------------
| Automatic route 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 optional inbound queues
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 );
}
}

Click here to download the complete instance code.

If you need it, go to the demo and check the effect!

I hope this article will help you with php programming.

This article introduces the deep calendar generation maze of the PHP tree and the * automatic pathfinding algorithm. The example shows how php implements A * lookup...

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.