You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

递归分割迷宫实现求助:样式异常与递归终止困惑

递归分割迷宫生成问题求助

我正在实现基于二维数组的递归分割迷宫算法,用.表示空地,#表示墙体。目前能生成墙体,但结果不符合迷宫样式;原本应该随机分割区域,但之前随机分割位置偏差过大,无奈改成了对半分割。推测问题出在墙体洞口位置设置上,同时不清楚如何正确终止递归。我的思路是不断将网格分割为4个区域并递归分割,代码如下:

const grider = (row, col) => {
    let grid = [];
    for (let r = 0; r < row; r++){
        let arr = [];
        for (let c = 0; c < col; c++){
            arr.push('.');
        }
        grid.push(arr);
    }
    return grid;
};

let gridi = grider(25, 25);

const anotherDiv = (grid) => {
    let arr = [];
    console.log(grid[0].length);

    for (let i = 0; i < grid[0].length; i++){
        grid[0][i] = '#';
        arr.push('(' + 0 + ',' + i + ')');
        grid[grid.length - 1][i] = "#";
        arr.push('(' + (grid.length - 1) + ',' + i + ')');
    }
    for (let i = 0; i < grid.length; i++){
       grid[i][0] = '#';
       arr.push('(' + i + ',' + 0 + ')');
       grid[i][grid[0].length - 1] = "#";
       arr.push('(' + i + ',' + (grid[0].length - 1) + ')');
    }

    let gridSize = [];
    gridSize.push([1, grid.length - 1, 1, grid[0].length - 1]) // 修正初始区域坐标,避免包含边界墙
    anotherRecursion(grid, gridSize);
};

anotherDiv(gridi);

function gethole(min, max) {
    return Math.floor(Math.random() * (max - min - 2)) + min + 1; // 洞口避开区域边界
}

function getRandom(min, max) {
    // 保证分割后子区域至少有2行/列的空地空间
    return Math.floor(Math.random() * (max - min - 2)) + min + 1;
}

function anotherRecursion(grid, gridSize){
    while(gridSize.length > 0) {
        const currentArea = gridSize.shift();
        const [top, bottom, left, right] = currentArea;
        
        // 终止条件:区域太小无法再分割(高度或宽度<=2)
        const height = bottom - top;
        const width = right - left;
        if(height <= 2 || width <= 2) continue;

        // 随机生成分割线位置
        const colSplit = getRandom(top, bottom); // 横向分割线(行号)
        const rowSplit = getRandom(left, right); // 纵向分割线(列号)

        // 生成横向分割墙的洞口(不能在分割线两端)
        const rowHole = gethole(left, right);
        // 生成纵向分割墙的洞口(不能在分割线两端)
        const colHole = gethole(top, bottom);

        // 绘制横向分割墙(colSplit行,从left到right)
        for(let i = left; i <= right; i++) {
            if(i !== rowHole) grid[colSplit][i] = '#';
        }
        // 绘制纵向分割墙(rowSplit列,从top到bottom)
        for(let i = top; i <= bottom; i++) {
            if(i !== colHole) grid[i][rowSplit] = '#';
        }

        // 拆分出四个子区域并加入队列
        gridSize.push(
            [top, colSplit, left, rowSplit], // 左上
            [top, colSplit, rowSplit + 1, right], // 右上
            [colSplit + 1, bottom, left, rowSplit], // 左下
            [colSplit + 1, bottom, rowSplit + 1, right] // 右下
        );
    }
    console.log(grid);
}

问题修复说明

  • 修正递归终止条件:改为判断当前区域的高度和宽度是否小于等于2,确保区域小到无法再分割(留足墙体和空地的空间),避免无限递归或生成过细的墙体。
  • 恢复并修正随机分割逻辑:getRandom函数现在会在有效范围内随机选择分割点,保证分割后的子区域有足够空间生成墙体和空地,不会出现偏差过大的问题。
  • 修正洞口位置规则:gethole函数确保洞口避开分割墙的两端,避免洞口和边界墙重合,同时保证每个分割墙只开一个洞,符合迷宫连通性要求。
  • 修正区域坐标与递归逻辑:初始区域坐标排除了边界墙,避免重复绘制;改用队列循环处理所有子区域,确保每个分割出的区域都能被递归处理,而不是只处理单个区域。
  • 调整分割墙绘制逻辑:修正了分割墙的绘制范围,确保墙体覆盖整个区域的分割线,同时正确保留洞口位置。

内容的提问来源于stack exchange,提问作者user16595186

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:49:55