我需要一些帮助,我的A *算法。它根本找不到路径。我认为这是我的堆,但我检查过,事实并非如此。它可能是我的算法吗?我使用2d数组来存储所有节点。该阵列基本上是一张地图,一些节点是岛屿(即不可行)。所以当我得到一个节点的后继者时,我会得到所有不是障碍的节点。A *算法不起作用
public void findPath(Node startNode, Node targetNode) throws HeapFullException, HeapEmptyException {
Heap<Node> openSet = new Heap<Node>(this.width*this.height);
ArrayList<Node> closeSet = new ArrayList<Node>();
int gScore;
startNode.gCost = 0;
startNode.hCost = getDistance(startNode,targetNode);
openSet.add(startNode);
while(!openSet.isEmpty()){
Node current = openSet.removeFirst();
if(current == targetNode){
break;
}
closeSet.add(current);
ArrayList<Node> successors = getNeighbours(current);
for (int i=0;i<successors.size();i++){
Node successor = successors.get(i);
if (closeSet.contains(successor)){
continue;
}
gScore = current.gCost + getDistance(current,successor);
if (!openSet.contains(successor)){
openSet.add(successor);
}
else if (gScore >= current.gCost){
continue;
}
successor.parent = current;
successor.gCost = gScore;
successor.hCost = getDistance(successor,targetNode);
}
}
}
private ArrayList<Node> getNeighbours(Node node) {
ArrayList<Node> neighbours = new ArrayList<Node>();
int x = node.gridX;
int y = node.gridY;
int[][] positions = {{x-1,y+1}, {x-1,y},{x-1,y-1},{x,y+1},{x,y-1},{x+1,y+1}, {x+1,y},{x+1,y-1}};
for (int i=0 ; i < 8; i ++){
if(positionExists(positions[i][0],positions[i][1])){
if (map[positions[i][1]][positions[i][0]].walkable){
neighbours.add(map[positions[i][1]][positions[i][0]]);
}
}
}
return neighbours;
}
getDistance的()是在运用的距离公式。
首先我会发布输入。确保有一条路径 – efekctive
我有一张打印的地图,并且总是有一条路径 – user1995933
getNeighbours(...)在哪里? – efekctive