当前位置: 首页 > 知识库问答 >
问题:

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

谢夜洛
2023-03-14

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

这是回溯算法:

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。

在10x10的迷宫中,这种情况似乎发生了大约1/10次。问题块没有被访问,因此算法根本不会处理它们。但是既然它经过它们,并且对邻居的每个方向都进行了测试,它们真的应该加入迷宫。那么会有什么问题呢?

共有1个答案

叶弘深
2023-03-14

问题是

Shuffle<Coordinate>(directions);

在每一步中,您都会按照方向对内容进行洗牌

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

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

因此,由于您使用DFS样式发现矩阵,因此,当您在父节点中迭代方向时,您也会访问它的所有子节点。同样,在访问每个子节点时洗牌方向,这可能会通过打乱方向中元素的当前顺序来随机破坏父节点中的迭代过程。

简单的例子

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
    }
}
 类似资料:
  • 我的任务是用回溯和递归的方法解决一个迷宫。这更多的是一个关于这个概念的概念问题。 回溯电话是如何接通的?从我所看到的所有示例来看,似乎递归总是在回溯步骤之前立即调用,所以回溯是无法实现的。谁能给我解释一下回溯步骤是怎么达到的?

  • 我在用递归解迷宫。我的矩阵是这样的 这是更大矩阵的原型。我的求解递归方法如下所示 你们可以注意到,这里我返回一个布尔值,如果我找到一条路径,它应该会给我一个真值。但它总是给我错误的答案。我不确定我在递归方法中犯的逻辑错误。方法如下 endX=3;endY=10;

  • 我正在开发高级培养皿网络编辑器/模拟器。首先,这里有一些词汇 圆圈=位置 矩形=过渡 就地整数 = 标记 过渡状态=防护 我被困在通过过渡的守卫。守卫是一个条件,如果你想执行转换,这需要是真的。我知道我应该以某种方式使用回溯,但我不知道在程序开始之前进入过渡的位置数,所以我不能使用循环,因为我不知道我需要多少个循环。 所以,我想从第一位获取第一个令牌,从第二位获取第一令牌,然后尝试通过守卫,如果通

  • 我正在尝试寻找到EndPotion的路径。这是一个递归函数。请帮助,我要自杀了。 这是给定的地图 我想递归地使用GetPath来到达上面地图中的EndPotion。参数是当前位置、结束位置和地图。对于这个例子,起始位置是(0,0)和结束,EndPotionis是(0,3),右上角。0代表墙壁,1代表路径。 我需要返回一个包含有效点的arraylist到结束位置。虽然我的数组大小始终为0,并且基本大

  • 本文向大家介绍Java实现走迷宫回溯算法,包括了Java实现走迷宫回溯算法的使用技巧和注意事项,需要的朋友参考一下 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍。设计一个程序,对任意设定的迷宫,求出一条从入口到出口的通路,或得出没有通路的结论。 (1) 根据二维数组,输出迷宫的图形。 (2) 探索迷宫的四个方向:RIGHT为向右,DOWN向下,LEFT向左,UP向上,输出从入口到

  • 我是C的新手,我目前正在一个项目中创建一个使用DFS算法生成的迷宫。 我已经成功地生成了一条路径,例如 如上所述, Source是初始单元,1是我根据随机邻居创建的路径,D是“死胡同”。所以,如果可能的话,我想回到S,从另一个方向开始。我该如何处理队列和堆栈?有人能解释一下吗?非常感谢你?