项目作者: shanghuiyang

项目描述 :
A-star algorithm implemented with Go.
高级语言: Go
项目地址: git://github.com/shanghuiyang/a-star.git
创建时间: 2020-10-07T16:06:42Z
项目社区:https://github.com/shanghuiyang/a-star

开源协议:MIT License

下载


A-Star

CI
License

A-Star algorithm implemented with Go.

Usage

see the example/main.go for complete usage.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/shanghuiyang/astar"
  5. "github.com/shanghuiyang/astar/tilemap"
  6. )
  7. // a map with 10 rows and 20 cols
  8. const strmap = `
  9. ####################
  10. # #
  11. # #
  12. # ######### #
  13. # #
  14. # ####### #
  15. # #
  16. # #
  17. # #
  18. ####################
  19. `
  20. func main() {
  21. // build a map from string
  22. m := tilemap.BuildFromStr(strmap)
  23. // define the origin and destination
  24. org := &astar.Point{X: 7, Y: 2}
  25. des := &astar.Point{X: 1, Y: 16}
  26. // find the path using a-star algorithm
  27. a := astar.New(m)
  28. path, err := a.FindPath(org, des)
  29. if err != nil {
  30. fmt.Printf("error: %v\n", err)
  31. return
  32. }
  33. // draw the tilemap with the path
  34. a.Draw()
  35. fmt.Printf("path: %v\n\n", path)
  36. }

output,

  1. ####################
  2. # .B #
  3. # . #
  4. # #########. #
  5. # ........ #
  6. # . ####### #
  7. # . #
  8. # A #
  9. # #
  10. ####################
  11. path: (7, 2) (6, 3) (5, 4) (4, 5) (4, 6) (4, 7) (4, 8) (4, 9) (4, 10) (4, 11) (4, 12) (3, 13) (2, 14) (1, 15) (1, 16)

More Cases

  1. ################ ################
  2. # # # #
  3. # A # # A #
  4. # ## # # . ## #
  5. # # # . #
  6. # B # # ........B #
  7. ################ ################
  8. ----------------------------------------
  9. ################ ################
  10. # # # #
  11. # A # # A.... #
  12. ####### ######## #######.########
  13. # # # . #
  14. # B # # ....B #
  15. ################ ################
  16. ----------------------------------------
  17. ################ ################
  18. # # # .. #
  19. # A # # # A . #. #
  20. # # # # .. # . #
  21. # # # # # . #
  22. # # B # # # ..B #
  23. ################ ################
  24. ----------------------------------------
  25. ################
  26. # # #
  27. # A # #
  28. # # # no way!
  29. # # #
  30. # # B #
  31. ################
  32. ----------------------------------------
  33. ################
  34. # #
  35. # A #
  36. # # no way!
  37. # #####
  38. # # B #
  39. ################