JavaScript迷宫求解器算法

Ale*_*vič 15 html javascript html5 maze

HTML

<div id="labirinth">
    <form style="text-align:center" name="forma1" autocomplete="on">
        <table style="margin:0 auto;">
            <tr>
                <td style="float:right;">Height:</td>
                <td><input type="text" id="height" name="height" autofocus="autofocus" maxlength="2" size="6" /></td>
            </tr>
            <tr>
                <td style="float:right;">Width:</td>
                <td><input type="text" id="width" name="width"  maxlength="2" size="6" /></td>
            </tr>
        </table>
    </form>
    <input type="button" alt="submit" onClick="datas();" value="New" style="margin-top:10px;" />
</div>
<pre id="out"></pre>
Run Code Online (Sandbox Code Playgroud)

JavaScript的

function datas() {

    var height = parseInt(document.getElementById("height").value);
    var width = parseInt(document.getElementById("width").value);

    document.getElementById('out').innerHTML = display(maze(height,width));
}

function maze(x,y) {
    var n=x*y-1;
    if (n<0) {alert("Bad numbers!");return;}
    var horiz=[]; 
        for (var j= 0; j<x+1; j++) horiz[j]= [];
    var verti=[]; 
        for (var j= 0; j<y+1; j++) verti[j]= [];

    var here= [Math.floor(Math.random()*x), Math.floor(Math.random()*y)];
    var path= [here];
    var unvisited= [];
    for (var j= 0; j<x+2; j++) {
        unvisited[j]= [];
        for (var k= 0; k<y+1; k++)
            unvisited[j].push(j>0 && j<x+1 && k>0 && (j != here[0]+1 || k != here[1]+1));
    }
    while (0<n) {
        var potential= [[here[0]+1, here[1]], [here[0],here[1]+1],
            [here[0]-1, here[1]], [here[0],here[1]-1]];
        var neighbors= [];
        for (var j= 0; j < 4; j++)
            if (unvisited[potential[j][0]+1][potential[j][1]+1])
                neighbors.push(potential[j]);
        if (neighbors.length) {
            n= n-1;
            next= neighbors[Math.floor(Math.random()*neighbors.length)];
            unvisited[next[0]+1][next[1]+1]= false;
            if (next[0] == here[0])
                horiz[next[0]][(next[1]+here[1]-1)/2]= true;
            else 
                verti[(next[0]+here[0]-1)/2][next[1]]= true;
            path.push(here= next);
        } else 
            here= path.pop();
    }
    return ({x: x, y: y, horiz: horiz, verti: verti});
}

function display(m) {
    var text= [];
    for (var j= 0; j<m.x*2+1; j++) {
        var line= [];
        if (0 == j%2)
            for (var k=0; k<m.y*4+1; k++)
                if (0 == k%4) 
                    line[k]= 'X';
                else
                    if (j>0 && m.verti[j/2-1][Math.floor(k/4)])
                        line[k]= ' ';
                    else
                        line[k]= 'X';
        else
            for (var k=0; k<m.y*4+1; k++)
                if (0 == k%4)
                    if (k>0 && m.horiz[(j-1)/2][k/4-1])
                        line[k]= ' ';
                    else
                        line[k]= 'X';
                else
                    line[k]= ' ';
        if (0 == j) line[1]=line[3]=' ',line[2]= '1';
        if (m.x*2-1 == j) line[4*m.y]= '2';
        text.push(line.join('')+'\r\n');

    }
    return text.join('');
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试在JavaScript中创建完全工作的迷宫生成器,而不使用HTML表格单元格.现在我对这个迷宫的创建求解器有问题.

问题:我的代码需要使用哪种迷宫求解算法?我该怎么做?我不需要整个算法 - 我只需要建议是否可以在这个迷宫生成器中使用迷宫求解器.

JSbin - http://jsbin.com/uwoyon/1

Ayb*_*btu 6

我建议的解决方案应该适用于您正在生成的迷宫,这是Dijkstra的算法.虽然我不确定horiz和verti参数如何定义迷宫结构,但Dijkstra的算法可以在你的情况下起作用,从"1"旁边的单元格开始并从那里构建.

它的运作方式是用每个细胞与起始细胞之间的细胞数标记每个细胞.对于3x3迷宫,第一个单元格将是:

x 1 xxxxxxxxx
x 1         x
x   xxxxxxxxx
x           x
x   xxxxxxxxx
x           2
xxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

从标记的单元格开始检查是否存在空的相邻单元格(未被墙壁阻挡),将单元格值增加1.对所有空单元格重复此过程:

x 1 xxxxxxxxx
x 1   2   3 x
x   xxxxxxxxx
x 2   3   4 x
x   xxxxxxxxx
x 3   4   5 2
xxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)

现在从靠近末端'2'的单元格向后工作.这表明该解决方案的路径长度为5步,因此从5开始,找到相邻的4,然后将3等返回到1.

注意:我建议使用队列标记单元格的递归解决方案:

1-用"1"标记第一个单元格并将其放入队列中.

2-从队列中取出单元格: - 检查合法邻居(未被墙壁阻挡的邻居)是否未标记. - 如果相邻单元格未标记,则使用当前单元格+ 1标记它. - 将相邻单元添加到队列中. - 对所有4个潜在邻居重复

3-重复步骤1和2,直到没有更多未标记的细胞.

编辑:这是使用我建议的解算器的小提琴,它与问题中的迷宫生成器无关,所以除非被问及,我不会详细介绍它是如何运作的(它有点粗糙,但它的实现应该如此很容易遵循).