-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (108 loc) · 2.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"log"
"reflect"
pr "github.com/kr/pretty"
)
type Map [][]int
type Point [2]int
type Node struct {
Parent *Node
Location Point
H, G, F int
}
var newPosition = [8]Point{
{0, -1}, {0, 1},
{-1, 0}, {1, 0},
{-1, -1}, {-1, 1},
{1, -1}, {1, 1},
}
func (n *Node) equalTwoNode(node *Node) bool {
return reflect.DeepEqual(n.Location, node.Location)
}
func aStar(m Map, start, end *Node) []Point {
var OpenNode []Node = []Node{*start}
var CloseNode []Node
for len(OpenNode) > 0 {
currentNode := OpenNode[0]
currentIndex := 0
for i, v := range OpenNode {
if v.F > currentNode.F {
currentNode = v
currentIndex = i
}
}
if currentNode.equalTwoNode(end) {
var path []Point
var current *Node = ¤tNode
for current != nil {
path = append(path, current.Location)
current = current.Parent
}
return path
}
// remove open node with Index and add to close Node
OpenNode = append(OpenNode[:currentIndex], OpenNode[currentIndex+1:]...)
CloseNode = append(CloseNode, currentNode)
var children []Node
for _, v := range newPosition {
nodePosition := Point{currentNode.Location[0] + v[0], currentNode.Location[1] + v[1]}
if nodePosition[0] > len(m)-1 ||
nodePosition[0] < 0 ||
nodePosition[1] > len(m[len(m)-1])-1 ||
nodePosition[1] < 0 {
continue
}
if m[nodePosition[0]][nodePosition[1]] != 0 {
continue
}
newNode := Node{Parent: ¤tNode, Location: nodePosition}
children = append(children, newNode)
}
for _, child := range children {
var is_close bool
for _, c := range CloseNode {
if child.equalTwoNode(&c) {
is_close = true
break
}
}
if is_close {
continue
}
child.G = currentNode.G + 1
child.H = (child.Location[0] - end.Location[0]*end.Location[0]) +
(child.Location[1] - end.Location[1]*end.Location[1])
child.F = child.G + child.H
OpenNode = append(OpenNode, child)
}
}
return []Point{}
}
func main() {
m := Map{
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
start := Node{Location: Point{0, 0}}
end := Node{Location: Point{7, 6}}
out := aStar(m, &start, &end)
ans := []Point{
{7, 6}, {6, 5},
{5, 4}, {4, 3},
{3, 3}, {2, 2},
{1, 1}, {0, 0}}
if !reflect.DeepEqual(ans, out) {
log.Fatalf("This Answer Not Equal:\n %# v \nYou Answer:\n %# v",
pr.Formatter(out),
pr.Formatter(ans))
}
log.Println("True Answer!")
}