2013-04-07 60 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