-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjps.go
More file actions
188 lines (154 loc) · 4.53 KB
/
jps.go
File metadata and controls
188 lines (154 loc) · 4.53 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"container/heap"
"math"
)
type Position struct {
X, Y int
}
type Node struct {
Position Position
G, H, F int
ParentDir [2]int
ForcedNeighborDir [2]int
Parent *Node
}
type PriorityQueue []*Node
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
return pq[i].F < pq[j].F
}
func (pq PriorityQueue) Swap(i, j int) {
pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Push(x interface{}) {
node := x.(*Node)
*pq = append(*pq, node)
}
func (pq *PriorityQueue) Pop() interface{} {
old := *pq
n := len(old)
node := old[n-1]
*pq = old[0 : n-1]
return node
}
func isWalkable(x, y int, matrix [][]int) bool {
return x >= 0 && y >= 0 && x < len(matrix) && y < len(matrix[0]) && matrix[x][y] == 0
}
func heuristic(a, b *Position) int {
// Using Manhattan distance as heuristic
return int(math.Abs(float64(a.X-b.X)) + math.Abs(float64(a.Y-b.Y)))
}
func findPathWithJPS(start, end Position, matrix [][]int) []Position {
startNode := &Node{Position: start}
endNode := &Node{Position: end}
openList := &PriorityQueue{}
heap.Init(openList)
heap.Push(openList, startNode)
for openList.Len() > 0 {
currentNode := heap.Pop(openList).(*Node)
if currentNode.Position == endNode.Position {
return reconstructPath(currentNode)
}
successors := findSuccessors(currentNode, endNode, matrix)
for _, successor := range successors {
heap.Push(openList, successor)
}
}
return nil
}
func reconstructPath(node *Node) []Position {
var path []Position
// Collect nodes from end to start
for node != nil {
path = append(path, node.Position)
node = node.Parent
}
// Reverse the path to get the correct order
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
}
return path
}
func findSuccessors(currentNode *Node, endNode *Node, matrix [][]int) []*Node {
var successors []*Node
x, y := currentNode.Position.X, currentNode.Position.Y
directions := getDirections(currentNode)
for _, dir := range directions {
dx, dy := dir[0], dir[1]
sx, sy, forcedNeighborDir, found := jump(x+dx, y+dy, dx, dy, endNode, matrix)
if found {
successor := &Node{Position: Position{X: sx, Y: sy}}
successor.G = currentNode.G + heuristic(¤tNode.Position, &successor.Position)
successor.H = heuristic(&successor.Position, &endNode.Position)
successor.F = successor.G + successor.H
successor.ParentDir = dir
successor.ForcedNeighborDir = forcedNeighborDir
successor.Parent = currentNode
successors = append(successors, successor)
}
}
return successors
}
func getDirections(node *Node) [][2]int {
if node.ParentDir == [2]int{0, 0} {
return [][2]int{
{0, -1}, {0, 1}, {-1, 0}, {1, 0},
{-1, -1}, {-1, 1}, {1, -1}, {1, 1},
}
}
pdx, pdy := node.ParentDir[0], node.ParentDir[1]
fndx, fndy := node.ForcedNeighborDir[0], node.ForcedNeighborDir[1]
if pdx != 0 && pdy != 0 {
directions := [][2]int{{pdx, pdy}, {pdx, 0}, {0, pdy}}
if fndx != 0 || fndy != 0 {
directions = append(directions, [2]int{fndx, fndy})
}
return directions
}
directions := [][2]int{{pdx, pdy}}
if fndx != 0 || fndy != 0 {
directions = append(directions, [2]int{fndx, fndy})
}
return directions
}
func jump(x, y, dx, dy int, endNode *Node, matrix [][]int) (int, int, [2]int, bool) {
for isWalkable(x, y, matrix) {
if x == endNode.Position.X && y == endNode.Position.Y {
return x, y, [2]int{}, true
}
if dx != 0 && dy != 0 {
if !isWalkable(x-dx, y, matrix) && !isWalkable(x, y-dy, matrix) {
return 0, 0, [2]int{}, false
}
if !isWalkable(x, y+dy*-1, matrix) && isWalkable(x+dx, y+dy*-1, matrix) {
return x, y, [2]int{dx, dy * -1}, true
}
if !isWalkable(x+dx*-1, y, matrix) && isWalkable(x+dx*-1, y+dy, matrix) {
return x, y, [2]int{dx * -1, dy}, true
}
_, _, _, found1 := jump(x+dx, y, dx, 0, endNode, matrix)
_, _, _, found2 := jump(x, y+dy, 0, dy, endNode, matrix)
if found1 || found2 {
return x, y, [2]int{}, true
}
} else if dx != 0 {
if !isWalkable(x, y-1, matrix) && isWalkable(x+dx, y-1, matrix) {
return x, y, [2]int{dx, -1}, true
}
if !isWalkable(x, y+1, matrix) && isWalkable(x+dx, y+1, matrix) {
return x, y, [2]int{dx, 1}, true
}
} else if dy != 0 {
if !isWalkable(x-1, y, matrix) && isWalkable(x-1, y+dy, matrix) {
return x, y, [2]int{-1, dy}, true
}
if !isWalkable(x+1, y, matrix) && isWalkable(x+1, y+dy, matrix) {
return x, y, [2]int{1, dy}, true
}
}
x += dx
y += dy
}
return 0, 0, [2]int{}, false
}