Why Wont Maze solver Code Work [duplicate]

随声附和 提交于 2019-12-24 15:23:58

问题


Sorry for the bad formatting and large amount of code, I am only a beginner and do not know how to diagnose the error down to a smaller amount of code. Where am i going wrong it just returns "found empty at 0, 0" and exits. I have viewed other code under this nature and cannot understand the answers given and need more elaboration on this specific code. Please Help

 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()

回答1:


No claims as to whether this is faster than the other answer, but if you want to return True/False if you have found the exit, then you can maintain a boolean for each of the recursive calls.

(Also, your print_grid method can be shorter)

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

Anyways, here are the modifications that I made to the program.

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



回答2:


return is the end point of a function, in your code you need to remove the return true because it is causing your program to exit prematurely. Adsitionally, you need an if case to deal with the cells that have been visited already (set to 'x').

I played around with it for a minute, the final result looks like this:

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)

Note that this code finds and visits every cell in the grid, and continues even after the end is found. If you want something to actually solve your maze and give you the steps necessary, you can make a few modifications to this code so that it stores the path you take. You can also research Breadth-First-Search as this is pretty much what you are using here.



来源:https://stackoverflow.com/questions/35545291/why-wont-maze-solver-code-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!