我一直停留在這個棘手的錯誤,在過去幾個小時,我在想,如果這裏有人可以幫助我。整數遞歸A的每個循環中被重置*算法
基本上我實現通過遞歸A *,我想每個節點(稱爲代碼中的一個瓦片)來存儲它已經通過先前的節點的數目的一個整數值。這是這樣的,一旦算法找到了出口,它可以返回並返回最短路線。
然而轉彎計數器正在每次遍歷函數時間復位。但是,如果我刪除行:
map[y][x].setID(path);
它細支起來,當然生成一個堆棧溢出錯誤,但我真的不能明白爲什麼這會導致問題。
代碼的主比特是在這裏:
private static Tile[][] findSquares(IntVector v, Tile[][] map, int wall, int empty, int end, int start, int path, int turns)
{
// System.out.println(turns);
if (!isHit)
{
for (int y = v.y - 1; y <= v.y + 1; y++)
{
for (int x = v.x - 1; x <= v.x + 1; x++)
{
if (map[y][x].id == end)
{
isHit = true;
}
else if (map[y][x].id != wall && map[y][x].id != path && map[y][x].id != end && !isHit && map[y][x].id != start)
{
map[y][x].turns++;
System.out.println(map[y][x].turns); //Always Results in 1
map[y][x].setID(path);
findSquares(new IntVector(x, y), map, wall, empty, end, start, path, turns);
break;
}
}
}
}
return map;
}
與表示節點瓦片。這裏是瓷磚類:
static private class Tile
{
int id;
int turns = 0;
Tile(int id)
{
this.id = id;
}
public void addTurn()
{
turns++;
}
public void setID(int id)
{
this.id = id;
}
public int getTurns()
{
return turns;
}
public Tile setTurns(int turns)
{
this.turns = turns;
return this;
}
}
也許這是關於瓦類是靜態的?
其中isHit定義?另外,A *通常使用優先級隊列和啓發式函數實現,但我沒有看到它們。 – Antimony 2013-04-07 19:15:40
你說你實現'A *',那麼你的啓發函數在哪裏?你使用哪個?請注意,'A *'算法僅僅是'Dijkstra'算法的一個普通實現,不同之處在於增加**啓發函數**以提高速度。一種可能的啓發是* as-the-crows-fly *,但也有其他可能性。 – Zabuza 2018-03-08 16:34:53
如果有幫助,[這裏](https://github.com/ZabuzaW/PathWeaver/blob/master/src/de/zabuza/pathweaver/network/algorithm/shortestpath/DijkstraShortestPathComputation.java)Dijkstra算法是用Java實現說明。和[這裏](https://github.com/ZabuzaW/PathWeaver/blob/master/src/de/zabuza/pathweaver/network/algorithm/shortestpath/AStarShortestPathComputation.java)是把它變成唯一需要改變的' A *',使用*作爲最烏鴉飛*從[這裏](https://github.com/ZabuzaW/PathWeaver/blob/master/src/de/zabuza/pathweaver/network/algorithm/metric/ StraightLineRoadTimeMetric.java)。 – Zabuza 2018-03-08 16:38:47