2017-02-26 74 views
0

我需要一些帮助,我的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的()是在运用的距离公式。

+0

首先我会发布输入。确保有一条路径 – efekctive

+0

我有一张打印的地图,并且总是有一条路径 – user1995933

+0

getNeighbours(...)在哪里? – efekctive

回答

0

只是评估一般情况:start节点等于目标节点,findPath根本不会提供任何信息。 openSet将被清空并关闭。

+0

startNode和targetNode将永远不会相同,也不会在数组中具有相同的位置。 – user1995933

+0

那么这是新的信息,并不排除targetNode是一个它将没有路径的岛屿。所以我会张贴你正在使用的输入。退出当天晚上 – efekctive

+0

注销并永不回复无用 – user1995933