1
1
package org .bk .ass .path ;
2
2
3
- import static java .lang .Math .abs ;
4
- import static java .lang .Math .max ;
5
- import static java .lang .Math .min ;
6
-
7
3
import java .util .ArrayList ;
8
4
import java .util .Collections ;
9
- import java .util .HashMap ;
10
- import java .util .HashSet ;
11
5
import java .util .List ;
12
- import java .util .Set ;
13
- import java .util .TreeSet ;
6
+ import java .util .PriorityQueue ;
14
7
15
- abstract class AbstractPathFinder {
8
+ import static java . lang . Math .*;
16
9
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 ;
20
14
final Position target ;
21
15
private final Map map ;
22
16
23
17
AbstractPathFinder (Position target , Map map ) {
24
18
this .target = target ;
25
19
this .map = map ;
20
+ nodes = new Node [map .getHeight () * map .getWidth ()];
26
21
}
27
22
28
23
Result searchFrom (Position start ) {
29
24
openQueue .add (new Node (start ));
30
25
Node best ;
31
- while ((best = openQueue .pollFirst ()) != null ) {
26
+ while ((best = openQueue .poll ()) != null ) {
32
27
if (best .position .equals (target )) {
33
28
List <Position > path = new ArrayList <>();
34
29
Node n = best ;
@@ -40,8 +35,9 @@ Result searchFrom(Position start) {
40
35
return new Result (best .g / 10f , path );
41
36
}
42
37
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 ;
45
41
if (best .parent == null ) {
46
42
addToOpenSet (best , jumpHorizontal (p .x , p .y , -1 ));
47
43
addToOpenSet (best , jumpHorizontal (p .x , p .y , 1 ));
@@ -91,20 +87,29 @@ Result searchFrom(Position start) {
91
87
return new Result (Float .POSITIVE_INFINITY , Collections .emptyList ());
92
88
}
93
89
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
+
94
98
protected abstract Position jumpVertical (int x , int y , int dy );
95
99
96
100
protected abstract Position jumpHorizontal (int x , int y , int dx );
97
101
98
102
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 ) {
100
105
Node node = new Node (parent , pos );
101
- Node existing = nodes . get ( pos ) ;
106
+ Node existing = nodes [ index ] ;
102
107
if (existing == null || existing .f > node .f ) {
103
108
if (existing != null ) {
104
109
openQueue .remove (existing );
105
110
}
106
111
openQueue .add (node );
107
- nodes . put ( pos , node ) ;
112
+ nodes [ index ] = node ;
108
113
}
109
114
}
110
115
}
@@ -137,6 +142,12 @@ private class Node implements Comparable<Node> {
137
142
final int g ;
138
143
final int f ;
139
144
145
+ Node () {
146
+ parent = null ;
147
+ position = null ;
148
+ g = f = 0 ;
149
+ }
150
+
140
151
Node (Position start ) {
141
152
parent = null ;
142
153
position = start ;
0 commit comments