【Pathfinding】Super simple A-star pathfinding algorithm implementation

A star pathfinding algorithm


The A-star pathfinding algorithm is an algorithm for calculating the shortest path, and calculates the optimal path by traversing all possible points around it.
It absorbs the advantages of Dijkstra's algorithm, sets weights for each side length, and continuously calculates the distance (G) from each vertex to the starting vertex to obtain the shortest route. It also draws on the greedy best-first search algorithm The advantage of continuously advancing to the target, and continuously calculating the distance (Heuristic distance) from each vertex to the target vertex, to guide the search queue to keep approaching the target, so as to search for fewer vertices and maintain the efficiency of pathfinding.

pseudocode:


1. Add the starting point to the OpenList;
2. Start a cycle to determine whether there is a node that can move forward in the OpenList;
3. Take the node with the smallest F value from the OpenList and put it into the Close List;
4. Determine whether the node has not reached the end point, and if so, jump out Loop, instead of continuing to add feasible nodes around the point to the OpenList;
5. Loop step 3 until the end is found or all OpenLists are traversed;

official:


f = g + h
f: pathfinding cost;
g: movement cost from the starting point; the calculation of G value can be expanded.
When going up, down, left, and right, the movement cost is 10
to go to the upper left, lower left, and upper right , and the lower right four grids, the movement cost is 14; that is, the movement cost of going diagonally is 1.4 times that of going straight.

h: estimated consumption from the end point, the most common and simplest method is to use the Manhattan distance to estimate
H = horizontal consumption from the current block to the end point + vertical consumption from the current block to the end point

OpenList; used to store nodes that can move forward and have not been calculated.
Close List: Used to store nodes that are no longer considered.

core code

public List<AstarNode> FindPath(int _startX, int _startY, int _endX, int _endY)
    {
        List<AstarNode> _path = new List<AstarNode>();
        m_openList.Clear();
        closeList.Clear();
        bool findPathFlag = false;

        AstarNode currNode = null;
        addToOpenList(null, _startX, _startY, _endX, _endY);//将起点加入OpenList;

        while (m_openList.Count > 0)
        {
            currNode = OpenList[0];//取F值最小的节点
            m_openList.Remove(currNode);//将当前节点移出OpenList
            closeList.Add(currNode);//将当前节点加入CloseList
            if (currNode == null)
            {
                break;
            }

            if (currNode.X == _endX && currNode.Y == _endY)//如果找到重点,退出循环
            {
                findPathFlag = true;
                break;
            }

            genAroundNode(currNode, _endX, _endY);//将周围满足条件的节点加入OpenList
        }
        if (!findPathFlag)
        {
            currNode = findNearPointFromList(closeList, endX, endY);//如果没有找到终点,在CloseList找一个最近的点。
        }
        if (currNode == null)
        {
            return null;
        }
        while (true)//递归父节点,找到所有路径
        {
            _path.Add(currNode);

            if (currNode.X == _startX && currNode.Y == _startY)
            {
                break;
            }
            currNode = currNode.Parent;
        }

        _path.Reverse();

        return _path;

    }

A star pathfinding algorithm Demo https://download.csdn.net/download/qq_33461689/14928453

Guess you like

Origin blog.csdn.net/qq_33461689/article/details/125535132