为什么不会迷宫解算器代码工作
Why Wont Maze solver Code Work
抱歉格式错误和代码量大,我只是初学者,不知道如何诊断错误到更小的代码量。我哪里出错了它只是 returns "found empty at 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()
return 是函数的终点,在您的代码中您需要删除 return true 因为它会导致您的程序过早退出。此外,您需要一个 if 案例来处理已经访问过的单元格(设置为 '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,因为这几乎就是您在这里使用的内容。
没有关于这是否比其他答案更快的说法,但是如果你想 return 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
抱歉格式错误和代码量大,我只是初学者,不知道如何诊断错误到更小的代码量。我哪里出错了它只是 returns "found empty at 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()
return 是函数的终点,在您的代码中您需要删除 return true 因为它会导致您的程序过早退出。此外,您需要一个 if 案例来处理已经访问过的单元格(设置为 '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,因为这几乎就是您在这里使用的内容。
没有关于这是否比其他答案更快的说法,但是如果你想 return 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