简体   繁体   中英

Understanding this implementation of A-star algorithm in Python

I am new to Python. I have this Code with me http://code.activestate.com/recipes/577519-a-star-shortest-path-algorithm/history/1/

its the A star algorithm code. I wanted to know how exactly is it generating the obstacles. Basically I have a black and white image. My task is to reach the end point from the start point avoiding the white spaces. Please help me Thank you!

The path-finding algorithm in function pathFind() is well-commented and decomposed. If there's something specific confusing you, tell us what it is. The important lines are after # generate moves (child nodes) in all possible directions , which admittedly is dense code and not very OO.

I suggest you reduce the parameters (m,n) to a 4x5 or 5x6 then step through its execution, either with pdb debugger (single-step or set breakpoints at key points), or hacking in as many print statements as you need:

# map matrix
n = 30 # horizontal size
m = 30 # vertical size

First try giving it your own custom maps to get a feel for how it works (modify the # fillout the map matrix... lines). Then run it interactively in the debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM