Skip to content

Commit 437d267

Browse files
author
Bytekeeper
committed
Fixed a bug where 2 equal costs overwrote each other.
1 parent 9b332e3 commit 437d267

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/main/java/org/bk/ass/path/AbstractPathFinder.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
package org.bk.ass.path;
22

3-
import static java.lang.Math.abs;
4-
import static java.lang.Math.max;
5-
import static java.lang.Math.min;
6-
73
import java.util.ArrayList;
84
import java.util.Collections;
9-
import java.util.HashMap;
10-
import java.util.HashSet;
115
import java.util.List;
12-
import java.util.Set;
13-
import java.util.TreeSet;
6+
import java.util.PriorityQueue;
147

15-
abstract class AbstractPathFinder {
8+
import static java.lang.Math.*;
169

17-
private final TreeSet<Node> openQueue = new TreeSet<>();
18-
private final java.util.Map<Position, Node> nodes = new HashMap<>();
19-
private final Set<Position> closed = new HashSet<>(400);
10+
abstract class AbstractPathFinder {
11+
private final Node CLOSED = new Node();
12+
private final PriorityQueue<Node> openQueue = new PriorityQueue<>();
13+
private final Node[] nodes;
2014
final Position target;
2115
private final Map map;
2216

2317
AbstractPathFinder(Position target, Map map) {
2418
this.target = target;
2519
this.map = map;
20+
nodes = new Node[map.getHeight() * map.getWidth()];
2621
}
2722

2823
Result searchFrom(Position start) {
2924
openQueue.add(new Node(start));
3025
Node best;
31-
while ((best = openQueue.pollFirst()) != null) {
26+
while ((best = openQueue.poll()) != null) {
3227
if (best.position.equals(target)) {
3328
List<Position> path = new ArrayList<>();
3429
Node n = best;
@@ -40,8 +35,9 @@ Result searchFrom(Position start) {
4035
return new Result(best.g / 10f, path);
4136
}
4237
Position p = best.position;
43-
boolean proceed = closed.add(p);
44-
if (proceed) {
38+
int index = idx(p);
39+
if (nodes[index] != CLOSED) {
40+
nodes[index] = CLOSED;
4541
if (best.parent == null) {
4642
addToOpenSet(best, jumpHorizontal(p.x, p.y, -1));
4743
addToOpenSet(best, jumpHorizontal(p.x, p.y, 1));
@@ -91,20 +87,29 @@ Result searchFrom(Position start) {
9187
return new Result(Float.POSITIVE_INFINITY, Collections.emptyList());
9288
}
9389

90+
private int idx(Position p) {
91+
return idx(p.y, map.getWidth(), p.x);
92+
}
93+
94+
private int idx(int y, int width, int x) {
95+
return y * width + x;
96+
}
97+
9498
protected abstract Position jumpVertical(int x, int y, int dy);
9599

96100
protected abstract Position jumpHorizontal(int x, int y, int dx);
97101

98102
private void addToOpenSet(Node parent, Position pos) {
99-
if (pos != null && !closed.contains(pos)) {
103+
int index;
104+
if (pos != null && nodes[index = idx(pos)] != CLOSED) {
100105
Node node = new Node(parent, pos);
101-
Node existing = nodes.get(pos);
106+
Node existing = nodes[index];
102107
if (existing == null || existing.f > node.f) {
103108
if (existing != null) {
104109
openQueue.remove(existing);
105110
}
106111
openQueue.add(node);
107-
nodes.put(pos, node);
112+
nodes[index] = node;
108113
}
109114
}
110115
}
@@ -137,6 +142,12 @@ private class Node implements Comparable<Node> {
137142
final int g;
138143
final int f;
139144

145+
Node() {
146+
parent = null;
147+
position = null;
148+
g = f = 0;
149+
}
150+
140151
Node(Position start) {
141152
parent = null;
142153
position = start;

0 commit comments

Comments
 (0)