c# - 递归回溯迷宫有时会留下瓷砖

标签 c# algorithm recursion unity3d maze

我有一个基本的回溯算法,可以为我生成迷宫。但有时它不会“访问”所有的图 block /单元格。我想知道出了什么问题,该算法会正确回溯,它应该检查每个图 block /单元格上的所有方向,但根本不会触及“未访问”的图 block /单元格。

这是回溯算法:

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;
        }
    }

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

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

在这里,它留下了一个没有被触及的瓷砖,但这次不是在侧面。 enter image description here

在 10x10 的迷宫中,这似乎发生了大约 1/10 次。问题图 block 未被访问,因此算法根本不处理它们。但由于它经过他们并且邻居的每个方向都经过测试,他们真的应该加入迷宫。那有什么问题呢?

最佳答案

问题是

Shuffle<Coordinate>(directions);

在每一步中,您都会随机播放 directions 中的内容

但是,还要记住,在每一步中,您都会遍历 directions 中的每个坐标。

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

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

简单的例子

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
    }
}

关于c# - 递归回溯迷宫有时会留下瓷砖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29342394/

相关文章:

c# - 如何获取多个类别的关键字匹配数?

c++ - 寻找最大和正整数子数组

java - 递归Java RLE解压方法【stackoverflow错误】

c# - 组合框下拉列表位于窗口屏幕下方。

c# - 将 Ninject 与 EasyNetQ/RabbitMQ 消息处理程序一起使用

c# - Control.SuspendLayout 和 Control.ResumeLayout 会计数吗?

c - 如何让我的代码在特定协调下找到解决方案?

c# - 使用 linq 查找 List<List<string>> 的多个值中的任何一个

python - Dijkstra 算法 - 最短路径中节点的错误顺序

c++ - 循环替换模板