PHP tree-generated maze and A * Automatic pathfinding algorithm, php maze

Source: Internet
Author: User

PHP tree-generated maze and A * Automatic pathfinding algorithm, php maze

PHP tree-generated maze and A * Automatic pathfinding Algorithm

The maze algorithm uses the deep traversal principle of the tree. 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.
Complete code has been uploaded, http://download.csdn.net/detail/hello_katty/8885779, here do some simple explanations, you need to think about it yourself. To put it bluntly, paste the class with the code maze:
/** Generate the Maze class * @ date 2015-07-10 * @ edit http://www.lai18.com * @ version 1 **/class Maze {// Maze Create private $ _ w; private $ _ h; private $ _ grids; private $ _ incluhistory; private $ _ incluhistory2; private $ _ targetSteps; // Construct public function Maze () {$ this-> _ w = 6; $ this-> _ h = 6; $ this-> _ grids = array ();} // set the public function set ($ width = 6, $ height = 6) of the maze) {if ($ width> 0) $ this-> _ w = $ widt H; if ($ height> 0) $ this-> _ h = $ height; return $ this;} // obtain the maze public function get () {return $ this-> _ grids;} // generates the maze public function create () {$ this-> _ init (); return $ this-> _ walk (rand (0, count ($ this-> _ grids)-1 ));} // obtain the 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 if ($ rand) shuffle ($ return ); if ($ n = 0) return $ return; if ($ n = 1) {return array_pop ($ return);} else {return array_slice ($ return, 0, $ n) ;}}/** generate a series of functions in the Maze */private function _ walk ($ startPos) {$ this-> _ history = array (); $ this-> _ export 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-> _ your history);} else {return false;} array_push ($ this-> _ Your 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); 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-> _ export 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

/** Path search 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;} public function set ($ width, $ height, $ grids) {$ this-> _ w = $ width; $ this-> _ h = $ height; $ this-> _ gr Ids = $ grids; return $ this;} // search for public function search in the maze ($ start = false, $ end = false) {return $ this-> _ search ($ start, $ end);}/** automatic pathfinding-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 = $ thi S-> _ 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 direction incoming 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-> _ 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 );}}

Additional reading Compilation of technical articles series of practical programming algorithms 

1 Introduction to algorithms: principles and implementation of selection and sorting

2 PHP Bubble Sorting Algorithm pen questions

3WordPress User Password calculation algorithm

4 PHP implements four common sorting algorithms

5. Intuitive data structure and algorithm demonstration

6. explore various knapsack algorithm problems in Depth

7 graphic insertion Sorting Algorithm

8 graphic Heap sorting Heap Sort Algorithm

9 C ++ algorithm simulation of Joseph's rings

10 rabbit issues with interesting Algorithms

11 JavaScript implementation of some common algorithms

12 interesting algorithms for monkey peach eating

13. Underlying algorithm efficiency of the square root sqrt () function

14 introduction to some algorithms related to the Binary Search Tree

15 Euclidean Algorithms

16 algorithm ideas for ant financial's pole crawling Problem

17 Important Algorithms in computer programming

18. Some common algorithm problems during the interview

19JavaScript sorting algorithm-hill sorting

20JavaScript sorting algorithm-heap sorting

21JavaScript sorting algorithm-Merge Sorting

22JavaScript sorting algorithm selection and sorting

Quick sorting by 23JavaScript Sorting Algorithm

Bubble Sorting by 24JavaScript Sorting Algorithm

Insert sorting by 25JavaScript Sorting Algorithm

26 comments and examples of merge sorting algorithms

27 Ugly uugly Number Search Algorithm

28. Algorithm for factorial of large numbers

29 C ++ implementation and performance testing of various sorting algorithms

30. A question about Hall in the introduction to Algorithms

31. Implement a stack and obtain its minimum element

32. Experience the KMP Algorithm

33. Quick thinking of interview algorithm questions

34 Algorithm for counter-clockwise rotation of Matrix

35Hash magic: Distributed Hash Algorithm

36Hash magic: consistent hash Algorithm

37 various implementation algorithms for string Reverse Order

38. Recursive quick sorting

39. Collect interview questions about some top software companies' classical algorithms

40 interesting algorithms: Monkey bananas

41 some mainstream Personalized Recommendation Algorithms

42 interesting algorithms: ratio of boys to women

43 interesting algorithms: virus Bottle Test for rats

44 Use PHP's built-in DES algorithm function for data encryption and decryption

45. Ninth: Relationship between data structures and algorithms

46. 12th words: What kind of algorithm is good?

47 million words: five basic features of the Algorithm

13th words: algorithm performance analysis

49. What is an algorithm?

50 million words: space complexity of Algorithms

51 million words: Worst Case and average condition of Algorithms

52. 14th words: how to calculate the time complexity of an algorithm

53 stack import operations

54. Ordered Stack: Stack's ordered Storage Structure

55 stack definition and general understanding

56 stack abstract data type ADT

57. STACK: Stack chain Storage Structure

Stack import operations

59. Obtain the top element of the stack of the sequential stack.

60. Stack exit operations

61 why use the stack data structure?

62 empty chain stack operation and Determination of empty chain Stack

63 recursion, one of the important applications of stack

How does 64 stack implement recursion?

65 chain stack outbound operations

66 chain stack initialization and traversal

67 talking about recursion: the idea of Recursion

68. recursion: two conditions to be met by recursion

69 talking about recursion: recursive judgment of the character string returning Phenomenon

70: Recursive Implementation of binary search algorithms

71. Recursive: the efficiency of Recursion

72. Recursive: recursion and loop

73 talking about recursion: Is loop and iteration the same thing?

74 talking about recursion: Learn about tail recursion from Fibonacci

75 talking about recursion: tail recursion and CPS

76 talking about recursion: supplement the knowledge of Continuation

77 talking about recursion: tail recursion in PHP and Its Optimization

78 talking about recursion: tail recursion optimization from the perspective of Assembly

79 knowledge in quick sorting: Start With guesses

80 knowledge in quick sorting: Let's look at the question of ball.

81 knowledge in fast sorting: Information Entropy

82 knowledge in quick sorting: The process of quick sorting

83 knowledge in quick sorting: Hall and quick sorting

84 knowledge in fast sorting: Implementation of Hall's fast sorting

85 knowledge in quick sorting: key element selection and algorithm efficiency

86 knowledge in quick sorting: randomization fast sorting

87Java programmers must master eight sorting algorithms

88RSA, DSA, and other encryption and decryption algorithms

89 permission Project Summary (1) Permission Design


Additional reading

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.