代码之家  ›  专栏  ›  技术社区  ›  Madmenyo

递归回溯迷宫有时会留下瓦片

  •  1
  • Madmenyo  · 技术社区  · 9 年前

    我有一个基本的回溯算法,可以为我生成迷宫。但有时它不会“访问”所有的区块/单元格。我想知道出了什么问题,算法确实正确地进行了回溯,它应该检查每个瓦片/单元的所有方向,但“未访问”的瓦片/单元根本不会被触摸。

    这是回溯算法:

    void GenerateMaze(Coordinate tilePos)
        {
            //Mark the current position visited
            tileMap[tilePos.x, tilePos.y].visited = true;
            //Randomize directions
            Shuffle<Coordinate>(directions);
            foreach(Coordinate d in directions)
            {
                //Check if the new position is within bounds
                if (tilePos.x + d.x >= 0 && tilePos.x + d.x < mapWidth && tilePos.y + d.y >= 0 && tilePos.y + d.y < mapHeight)
                {
                    //Check if the tile is already visited
                    if (!tileMap[tilePos.x + d.x, tilePos.y + d.y].visited)
                    {
                        //Carve through walls from this tile to next
                        Carve(tilePos, d);
                        //Recursively call this method on the next tile
                        GenerateMaze(new Coordinate(tilePos.x + d.x, tilePos.y + d.y));
                    }
                }
            }
        }
    

    如果您感兴趣,这是 Carve 方法:

    private void Carve(Coordinate position, Coordinate direction)
        {
            if (direction.Equals(new Coordinate(-1, 0)))
            {
                Debug.Log("Carving West from: ");
                tileMap[position.x, position.y].west = true;
                tileMap[position.x + direction.x, position.y + direction.y].east = true;
            }
            else if (direction.Equals(new Coordinate(1, 0)))
            {
                tileMap[position.x, position.y].east = true;
                tileMap[position.x + direction.x, position.y + direction.y].west = true;
            }
            else if (direction.Equals(new Coordinate(0, -1)))
            {
                tileMap[position.x, position.y].south = true;
                tileMap[position.x + direction.x, position.y + direction.y].north = true;
            }
            else if (direction.Equals(new Coordinate(0, 1)))
            {
                tileMap[position.x, position.y].north = true;
                tileMap[position.x + direction.x, position.y + direction.y].south = true;
            }
        }
    

    它只是根据算法的方向将正确的墙标志设置为true。

    在下图中,您可以看到迷宫有3个“未访问”的瓷砖。这主要发生在角落。 enter image description here

    在这里,它留下了一块未被触摸的瓷砖,但这次不是在侧面。 enter image description here

    在10x10的迷宫中,这种情况似乎发生了1/10次。问题区块保持未访问状态,因此算法根本不会处理它们。但由于它经过了他们,而且邻居的每个方向都经过了测试,他们真的应该加入迷宫。那么有什么问题呢?

    1 回复  |  直到 9 年前
        1
  •  3
  •   Pham Trung    9 年前

    问题是

    Shuffle<Coordinate>(directions);
    

    在每一步中,您都会在 directions

    但是,还要记住,在每个步骤中 方向

    foreach(Coordinate d in directions)
    {
         //Visit child node
    }
    

    所以,因为您正在使用 DFS 因此,当您迭代 方向 在父节点中,还可以访问其所有子节点。再一次, shuffling 这个 方向 在访问每个子节点时,这可能会打乱元素的当前顺序,从而随机破坏父节点中的迭代过程 方向 .

    简单的例子

    In parent, directions order is (0,1,2,3)
    
    Visit first child (direction 0)-> shuffle directions (1,0,2,3)
    
    Go back to parent node, now you will skip one node (direction 1), as the directions content has been changed.
    

    将此DFS更改为 BFS 将解决此问题。

    伪代码:

    Queue<Coordinate> q;
    q.add(start)
    while(q is not empty){
        Coordinate point = q.dequeue();
        shuffle directions
        for(each direction in directions){
            Add unvisited child node into q
        }
    }