我正在嘗試編寫(xiě) A* 搜索算法,但似乎無(wú)法使其正常工作。我正在從維基百科復(fù)制偽代碼。我的代碼似乎只是搜索每個(gè)可能的節(jié)點(diǎn)。這是我的 showPath() 函數(shù):public void showPath() {Nodes current = end;while(current.cameFrom!=null) { current.isPath = true; current = current.cameFrom;}}起始節(jié)點(diǎn)的 comeFrom 為 null,因?yàn)檫@是默認(rèn)值。public void A_Star() {PriorityQueue<Nodes> closedSet = new PriorityQueue<Nodes>();PriorityQueue<Nodes> openSet = new PriorityQueue<Nodes>();closedSet.clear();openSet.clear();start.gScore = 0;openSet.add(start);start.fScore = getDist(start,end);while(!(openSet.size() ==0)) { Nodes curr = openSet.poll(); if(curr.x == end.x && curr.y == end.y) { showPath(); } closedSet.add(curr); for(int i=0;i<curr.getNeighbourCount();i++) { Nodes neighbour = curr.getNeighbour(i); if(closedSet.contains(neighbour)) { continue; } //isPassable is a boolean that is false if the Nodes is an obstacle if(!openSet.contains(neighbour) && neighbour.isPassable) { openSet.add(neighbour); } //It's a grid so every point is a distance of 1 from it's neighbours else if((curr.gScore+1)>= neighbour.gScore){ continue; } neighbour.cameFrom = curr; neighbour.gScore = curr.gScore+1; neighbour.fScore = neighbour.gScore + getDist(neighbour,end); }}}編輯:我的 getDist 函數(shù)public int getDist(Nodes node1, Nodes node2) { return ( Math.abs(node1.x - node2.x) + Math.abs(node1.y - node2.y));}
添加回答
舉報(bào)
0/150
提交
取消