我整个周末都玩这个.我试图将节点存储在PriorityQueue数据结构中.我的astar函数似乎没有做它应该做的事情.有人介意看看吗?
public void aStar(Node from, Node to) {
PriorityQueue<Node> exploreList = new PriorityQueue<Node>();
ArrayList<Node> visited = new ArrayList<Node>();
ArrayList<Node> successors = new ArrayList<Node>();
Node current = from;
System.out.println(current.getName());
while (current != to) {
successors = current.getConnected();
Collections.sort(successors);
for (Node n : successors) {
if (!visited.contains(n)) {
exploreList.add(n);
}
for (Node n1 : successors) {
if (n.fSum() > n1.fSum()) {
exploreList.remove(n);
exploreList.add(n1);
}
}
}
visited.add(current);
current = exploreList.remove();
System.out.println(current.getName());
}
Run Code Online (Sandbox Code Playgroud)
节点类在这里
public class Node implements Comparable {
private String name; …Run Code Online (Sandbox Code Playgroud)