2013-04-07 59 views
0

我一直停留在這個棘手的錯誤,在過去幾個小時,我在想,如果這裏有人可以幫助我。整數遞歸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; 
    } 
} 

也許這是關於瓦類是靜態的?

+0

其中isHit定義?另外,A *通常使用優先級隊列和啓發式函數實現,但我沒有看到它們。 – Antimony 2013-04-07 19:15:40

+0

你說你實現'A *',那麼你的啓發函數在哪裏?你使用哪個?請注意,'A *'算法僅僅是'Dijkstra'算法的一個普通實現,不同之處在於增加**啓發函數**以提高速度。一種可能的啓發是* as-the-crows-fly *,但也有其他可能性。 – Zabuza 2018-03-08 16:34:53

+0

如果有幫助,[這裏](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

回答

0

的問題不在於轉計數器是被「復位」,那就是你永遠遞增一次以上。其中turns僅增加分支時發生id != path,但你設置idpath隨即,所以它不會再增加。

什麼你可能打算是 map[y][x].turns = map[v.y][v.x].turns + 1;

無論如何,即使您修復的距離計算,你的代碼幾乎類似於A *。它看起來像你的代碼實際上做的是深度優先搜索,隱式地在程序調用堆棧上維護你的搜索堆棧。

A *算法包括保持待搜索節點的優先級隊列,並使用啓發函數加上當前距離來計算插入節點的新的優先級。

+0

啊謝謝你的幫助。我認爲可能會放棄所有這些,並嘗試使用更好的A *版本。 – Derek 2013-04-07 20:23:52