LUA implementation of a star routing algorithm __lua

Source: Internet
Author: User

A * search algorithm is commonly known as a star algorithm. This is a graph on the plane, there are multiple nodes of the path to find the lowest cost of the algorithm.

Here is a predecessor of the article, very suitable for beginners to learn Mo Shui: A Star Search algorithm introduced http://www.cnblogs.com/zhoug2020/p/3468167.html.

The understanding of a-star algorithm is to start with the formula F = G + H: On the node map, each step of the operation, so that the distance has been gone + distance to the end of the minimum distance. The specific implementation is to maintain an open table and a closed table. (see an article with recursive implementation, is also very good.) )

It took a little time, and also a reference to some other people's implementation, with LUA to achieve their own a *.
Complete LUA code, please download it to my github. Https://github.com/MagicDavid20/LuaProj/blob/master/DOD/src/app/arithmetic/AStarFindRoute.lua This is the key implementation:

while (Table.nums (open_list) > 0) do
    current_pos = open_list[1]
    table.remove (open_list, 1)
    open_map[ Current_pos.key] = nil
    if is_same_p (Current_pos, End_pos) then return
        Makepath (current_pos)
    else
        Closed_map[current_pos.key] = Current_pos local
        nextpoints = getnextpoints (current_pos)
        for i = 1, #nextPoints Do local
            nextpoint = nextpoints[i]
            if (open_map[nextpoint.key] = = nil) and (closed_map[nextpoint.key] = = nil) D (Is_barrier (Nextpoint, barrier_list) = = False) then
                open_map[nextpoint.key] = nextpoint
                Table.insert (OPEN _list, Nextpoint) End-
        table.sort (open_list, Compare_func)
End return nil
Here are the test cases


(Picture from Mo Shui thousand stream: A star seek an algorithm to introduce http://www.cnblogs.com/zhoug2020/p/3468167.html, the data is simulate this picture.) )

 local prop = {} prop.x = 7 Prop.y = 6 Prop.startpos = {x = 2, y = 3} PROP.E Ndpos = {x = 6, y = 2} prop.barragelist = {} Prop.barragelist[1] = {x = 4, y = 2} prop.barragelist[2] = { x = 4, y = 3} Prop.barragelist[3] = {x = 4, y = 4} Prop.barragelist[4] = {x = 4, y = 5} prop.barragelist[
        5] = {x = 2, y = 2} Local Path = Astarfindroute.init (prop) Local src = "' For I, point in ipairs (path) do src = src.. Point.key ... " -> ' End print (' David say path is '. SRC) 

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.