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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
| import java.util.*;
public class AStarPathFinder {
protected final Vec2 maxAxis;
protected final List<Vec2> obstacles;
public AStarPathFinder(final Vec2 maxAxis, final List<Vec2> obstacles) { this.maxAxis = maxAxis; this.obstacles = Collections.unmodifiableList(obstacles); }
public record Vec2(float x, float y) {
@Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null || getClass() != obj.getClass()) return false;
Vec2 vec = (Vec2) obj; return Math.abs(vec.x - x) < 1e-6 && Math.abs(vec.y - y) < 1e-6; }
@Override public int hashCode() { return Objects.hash(Math.round(x), Math.round(y)); }
@Override public String toString() { return String.format("Vector(%.1f, %.1f)", x, y); } }
public static class Node {
Vec2 position;
Node parent;
float g;
float h;
float f;
public Node(Vec2 position, Node parent, float g, float h) { this.position = position; this.parent = parent; this.g = g; this.h = h; this.f = g + h; } }
public List<Vec2> findPath(Vec2 start, Vec2 end) { if (isBounds(start) || isBounds(end)) return Collections.emptyList();
if (isObstacle(start) || isObstacle(end)) return Collections.emptyList();
if (start.equals(end)) return Collections.singletonList(start);
PriorityQueue<Node> openQueue = new PriorityQueue<>((l, r) -> Float.compare(l.f, r.f)); Map<Vec2, Node> openMap = new HashMap<>();
Set<Vec2> closed = new HashSet<>();
Node startNode = new Node(start, null, 0, heuristic(start, end)); openQueue.add(startNode); openMap.put(start, startNode);
int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
while (!openQueue.isEmpty()) { Node currentNode = openQueue.poll(); Vec2 currentPos = currentNode.position; openMap.remove(currentPos);
if (currentPos.equals(end)) return reconstructPath(currentNode);
closed.add(currentPos);
for (int[] dir : directions) {
float newX = currentPos.x + dir[0]; float newY = currentPos.y + dir[1]; Vec2 neighborPos = new Vec2(newX, newY);
if (isBounds(neighborPos)) continue;
if (isObstacle(neighborPos)) continue;
if (closed.contains(neighborPos)) continue;
if (dir[0] != 0 && dir[1] != 0) { Vec2 horizontal = new Vec2(currentPos.x + dir[0], currentPos.y); Vec2 vertical = new Vec2(currentPos.x, currentPos.y + dir[1]);
if (isObstacle(horizontal) || isObstacle(vertical)) { continue; } }
float cost = (dir[0] != 0 && dir[1] != 0) ? 1.414f : 1.0f; float tentative = currentNode.g + cost;
Node active = openMap.get(neighborPos);
if (Objects.isNull(active)) { float h = heuristic(neighborPos, end); Node newNode = new Node(neighborPos, currentNode, tentative, h); openQueue.add(newNode); openMap.put(neighborPos, newNode); } else if (tentative < active.g) { active.parent = currentNode; active.g = tentative; active.f = active.g + active.h;
openQueue.remove(active); openQueue.add(active); } } }
return Collections.emptyList(); }
public boolean isBounds(final Vec2 pos) { return pos.x < 0 || pos.x > maxAxis.x || pos.y < 0 || pos.y > maxAxis.y; }
public boolean isObstacle(final Vec2 pos) { return obstacles.contains(pos); }
public float heuristic(final Vec2 cur, final Vec2 end) { float dx = end.x() - cur.x(); float dy = end.y() - cur.y(); return (float) Math.sqrt(dx * dx + dy * dy); }
public List<Vec2> reconstructPath(final Node node) { List<Vec2> path = new ArrayList<>(); Node current = node;
while (Objects.nonNull(current)) { path.add(current.position); current = current.parent; }
Collections.reverse(path); return path; }
public static void main(String[] args) { Vec2 map = new Vec2(19, 19);
List<Vec2> obstacles = Arrays.asList( new Vec2(2, 1), new Vec2(4, 2), new Vec2(6, 4), new Vec2(8, 6), new Vec2(12, 8), new Vec2(16, 12), new Vec2(18, 16) );
AStarPathFinder finder = new AStarPathFinder(map, obstacles);
Vec2 start = new Vec2(0, 0); Vec2 end = new Vec2(9, 9);
List<Vec2> paths = finder.findPath(start, end);
if (paths.isEmpty()) { System.err.println("未找到有效路径"); } else { System.out.printf("寻路成功, 共需要移动 %d 个节点: %n", paths.size()); for (Vec2 pos : paths) { System.out.printf(" - (%s,%s)%n", pos.x, pos.y); } } } }
|