Python - 回溯迷宫生成递归函数理解

时间:2018-03-29 09:48:31

标签: python python-3.x recursion depth-first-search backtracking

我一直在寻找在python中创建迷宫的方法 我在rosettacode处看到了以下代码 我知道代码使用递归来构建迷宫 我理解代码行,知道我正在阅读什么,我想使用该代码,但我对这段代码缺乏重要的理解。

此代码中递归函数的准确剂量知道何时停止?

from random import shuffle, randrange

def make_maze(w = 16, h = 8):
    vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
    ver = [["|  "] * w + ['|'] for _ in range(h)] + [[]]
    hor = [["+--"] * w + ['+'] for _ in range(h + 1)]

    def walk(x, y):
        vis[y][x] = 1

        d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
        shuffle(d)
        for (xx, yy) in d:
            if vis[yy][xx]: continue
            if xx == x: hor[max(y, yy)][x] = "+  "
            if yy == y: ver[y][max(x, xx)] = "   "
            walk(xx, yy)

    walk(randrange(w), randrange(h))

    s = ""
    for (a, b) in zip(hor, ver):
        s += ''.join(a + ['\n'] + b + ['\n'])
    return s

if __name__ == '__main__':
    print(make_maze())

1 个答案:

答案 0 :(得分:0)

将调试打印应用于您的代码:

from random import shuffle, randrange

def make_maze(w = 3, h =3):
    vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
    ver = [["|  "] * w + ['|'] for _ in range(h)] + [[]]
    hor = [["+--"] * w + ['+'] for _ in range(h + 1)]

    def debugPrint():
        print("-"*16)
        s = ""
        for (a, b) in zip(hor, ver):
            s += ''.join(a + ['\n'] + b + ['\n'])
        print(s )

        for r in vis:
            print(r) 


    def walk(x, y):
        debugPrint()

        vis[y][x] = 1

        d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
        shuffle(d)
        for (xx, yy) in d:
            if vis[yy][xx]: continue
            if xx == x: hor[max(y, yy)][x] = "+  "
            if yy == y: ver[y][max(x, xx)] = "   "

            walk(xx, yy)



    walk(randrange(w), randrange(h))

    s = ""
    for (a, b) in zip(hor, ver):
        s += ''.join(a + ['\n'] + b + ['\n'])
    return s

if __name__ == '__main__':
    print(make_maze())

想象发生了什么:

----------------
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+--+--+--+


[0, 0, 0, 1]
[0, 0, 0, 1]
[0, 0, 0, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+  +--+--+
|  |  |  |
+--+--+--+


[0, 0, 0, 1]
[1, 0, 0, 1]
[0, 0, 0, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+  +--+--+
|     |  |
+--+--+--+


[0, 0, 0, 1]
[1, 0, 0, 1]
[1, 0, 0, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+  +--+--+
|        |
+--+--+--+


[0, 0, 0, 1]
[1, 0, 0, 1]
[1, 1, 0, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|  |  |  |
+--+--+--+
|  |  |  |
+  +--+  +
|        |
+--+--+--+


[0, 0, 0, 1]
[1, 0, 0, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|  |  |  |
+--+--+  +
|  |  |  |
+  +--+  +
|        |
+--+--+--+


[0, 0, 0, 1]
[1, 0, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|  |     |
+--+--+  +
|  |  |  |
+  +--+  +
|        |
+--+--+--+


[0, 0, 1, 1]
[1, 0, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|        |
+--+--+  +
|  |  |  |
+  +--+  +
|        |
+--+--+--+


[0, 1, 1, 1]
[1, 0, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
----------------
+--+--+--+
|        |
+--+  +  +
|  |  |  |
+  +--+  +
|        |
+--+--+--+


[1, 1, 1, 1]
[1, 0, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]

最终输出:

+--+--+--+
|        |
+--+  +  +
|  |  |  |
+  +--+  +
|        |
+--+--+--+