Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man
In the previous article, we studied the basic concepts of the astar algorithm. This article introduces the specific implementation of the astar algorithm.
First, the function starts from the findpath function. The initialization content is as follows:
First, assign the current location and target location to the member variables of this class, then pass our map variables to the member variable map, and initialize the open, close, and path lists respectively, the path and close lists are empty first, and the open list is convenient for heap sorting (the index is multiplied by two or 0 when the index starts from 0 During Heap sorting, which causes inconvenience ). Then add the start point to the open list. The geth estimation function is involved here, because the cost to the start point is 0. The estimated function geth from the start point to the end is as follows:
The estimation function we selected is relatively simple, that is, calculate the vertical and horizontal index values from the current point to the target point, multiply it by 10, and we set the price between neighboring cells to 10, the cost of the diagonal line is 14 (10 * Root 2 ). Go back to findpath and start searching.
We traverse the elements in the open list. First, we use the fromopentoclose () function to retrieve the first element in the open list (the first element is invalid, as mentioned above) delete and add it to the close list. The fromopentoclose function is as follows:
First, add the first element from open to the close function, and then replace the first function in the open list with the last function in the open function in the removefromopen function, next, perform the heap sorting. Here we will briefly talk about the heap sorting, that is, regard the entire array as a tree structure, and then ensure that the parent node is smaller than the F value of the child node. In this way, the first element is the best
If there are any errors, I hope you can correct them more.
Next, continue to the astar algorithm game instance