为什么不迷宫求解器代码工作

时间:2016-02-22 03:21:04

标签: python function maze

对于错误的格式化和大量代码,我很抱歉,我只是一个初学者,不知道如何将错误诊断为较少量的代码。我在哪里出错它只返回“在0,0找到空”并退出。我已经查看过这种性质的其他代码,无法理解给出的答案,需要对此特定代码进行更详细的说明。请帮忙

 grid = [["T", " ", " ", "#", "#", "#"],
        ["#", "#", " ", " ", " ", "#"],
        ["#", " ", " ", "#", " ", "#"],
        ["#", "#", "#", " ", " ", "#"],
        ["#", "#", " ", " ", "#", " "],
        ["#", "#", "#", "#", "#", "E"]]

def print_grid():
    pr_grid = ""
    for key in grid:
        for num in key:
            pr_grid += str(num)
            pr_grid += " "
        print pr_grid
        pr_grid = ""

    print pr_grid

run = True

def main(x, y):
    if grid[x][y] == " " or grid[x][y] == "T":
        print "Found empty at %d %d" % (x, y)
        grid[x][y] = "x"
    elif grid[x][y] == "#":
        print "Found wall at %d %d" % (x, y)
    elif grid[x][y] == "E":
        print "Found exit at %d %d" % (x, y)

    if y < len(grid)-1:
        main(x, y + 1)
    if y > 0:
        main(x, y - 1)
    if x < len(grid[x])-1:
        main(x + 1, y)
    if x > 0:
        main(x - 1, y)


print_grid()

main(0, 0)

print_grid()

2 个答案:

答案 0 :(得分:1)

return是函数的结束点,在你的代码中你需要删除return true,因为它会导致程序过早退出。在理论上,你需要一个if case来处理已经访问过的单元格(设置为'x')。

我玩了一会儿,最终结果如下:

def main(x, y):
    if grid[x][y] == " " or grid[x][y] == "T":
        print "Found empty at %d %d" % (x, y)
        grid[x][y] = "x"
    elif grid[x][y] == "#":
        print "Found wall at %d %d" % (x, y)
        return
    elif grid[x][y] == "E":
        print "Found exit at %d %d" % (x, y)
        return 
    else: return

    if y < len(grid)-1:
        main(x, y + 1)
    if y > 0:
        main(x, y - 1)
    if x < len(grid[x])-1:
        main(x + 1, y)
    if x > 0:
        main(x - 1, y)

请注意,此代码会查找并访问网格中的每个单元格,并在找到结束后继续。如果您想要实际解决迷宫问题并为您提供必要的步骤,您可以对此代码进行一些修改,以便它存储您所采用的路径。您也可以研究Breadth-First-Search,因为这几乎就是您在这里使用的内容。

答案 1 :(得分:1)

没有声明这是否比其他答案更快,但如果你想找到退出时想要返回True / False,那么你可以为每个递归调用维护一个布尔值。

(另外,您的print_grid方法可以更短)

def print_grid():
    print "\n".join(' '.join(row) for row in grid)

无论如何,这是我对该程序所做的修改。

def main(x, y):
    # Check going out of bounds
    if y < 0 or y >= len(grid): 
        return False
    if x < 0 or x >= len(grid[y]):
        return False

    if grid[x][y] == "E":
        print "Found exit at %d %d" % (x, y)
        return True
    elif grid[x][y] == "#":
        print "Found wall at %d %d" % (x, y)
        return False
    elif grid[x][y] == " " or grid[x][y] == "T":
        print "Found empty at %d %d" % (x, y)
        grid[x][y] = "x"
        # no return, we want to continue searching
    else: # catch invalid characters
        return False

    found = False
    # "Bubble-up" the results from searching for the exit 
    # Also limit the search space by keeping track if the exit was found
    if y < len(grid)-1 and not found:
        found = main(x, y + 1)
    if y > 0 and not found:
        found = main(x, y - 1)
    if x < len(grid[x])-1 and not found:
        found = main(x + 1, y)
    if x > 0 and not found:
        found = main(x - 1, y)

    return found