我正在编写一个导入符号地图并递归导航地图的程序。它完成了它几乎可以工作,但无论出于何种原因,地图总是跳过一个地方,我无法找到原因。它应该使用' X标记正确的路径,并且不使用' +'。如果有人可以检查出来并找到我错过的错误,那将非常感谢!
当您使用网格运行代码时,它也会保留一些字符不变,无论我在哪里放置代码,它都会完全忽略它。我尝试过像:
for i in grid:
for j in i:
if j == "|":
j = "+"
if j == "#":
j = "+"
我运行的代码是:
grid = []
def buildGrid ():
with open("Maze.txt" , "r") as f:
for line in f:
chars = []
for c in line:
if c != "\n":
chars.append(c)
grid.append(chars)
freeMouse(0, 0)
def showPath ():
result = ""
for i in grid:
for j in i:
result += " "
result += j
result += "\n"
print(result)
def freeMouse (x, y):
if grid[y][x] == "O":
grid[0][0] = "S"
showPath()
elif grid[y][x] == "X":
return False
elif grid[y][x] == ("|"):
grid[y][x]= "+"
return False
elif grid[y][x] == ("+"):
return False
print("false")
grid[y][x] = "X"
if x != 0:
if y != 0:
left = freeMouse (x-1, y)
up = freeMouse(x, y -1)
if len(grid) == y + 1:
if len(grid[y]) != x+1:
right = freeMouse (x + 1, y)
if not right:
grid[y][x+1] = "+"
else:
if len(grid[y]) == x+1:
down = freeMouse(x, y+1)
else:
down = freeMouse(x, y+1)
right = freeMouse (x + 1, y)
if not right:
grid[y][x+1] = "+"
if not down:
grid[y+1][x] = "+"
if not up:
grid[y-1][x] = "+"
if not left:
grid[y][x-1] = "+"
else:
if len(grid[y])== x + 1:
left = freeMouse (x-1, y)
down = freeMouse (x, y+1)
else:
left = freeMouse (x-1, y)
down = freeMouse (x, y+1)
right = freeMouse (x+1, y)
if not right:
grid[y][x+1] = "+"
if not down:
grid[y+1][x] = "+"
if not left:
grid[y][x-1] = "+"
else:
if y != 0:
if len(grid) == y + 1:
up = freeMouse(x,y-1)
right = freeMouse (x+1, y)
else:
up = freeMouse(x, y-1)
down = freeMouse(x, y+1)
right = freeMouse(x + 1, y)
if not down:
grid[y+1][x] = "+"
if not up:
grid[y-1][x] = "+"
else:
down = freeMouse(x, y+1)
right = freeMouse(x+1, y)
if not down:
grid[y+1][x] = "+"
buildGrid()
答案 0 :(得分:0)
我尝试运行您的代码并获得以下输出
S X + + X X X X X X
X + + X + X + + + X
+ X + + + X | # + X
+ X X X X X # # + X
+ + + + + | | | + X
+ + | + X X X X + X
+ + + X X | + X X X
+ + + X X # | + + +
+ + + + X + + X X X
+ + + + X X X X + O
我看到几个未处理的备用#。 我觉得这个问题出现在return语句中 - 你应该记住'not None == True' 您可以通过打开python控制台并键入以下内容来测试它
def func():
return
print not func(); // This returns true
因此,我建议删除'if not up','if not down'等语句。
if grid[y][x] == "O":
grid[0][0] = "S"
showPath()
# To stop from endless recursion -- don't go back to traversed path
elif grid[y][x] == "X":
return False
# Don't run into wall
elif grid[y][x] == ("|"):
grid[y][x]= "+"
return False
# Don't run into locations already not traversable
elif grid[y][x] == ("+"):
grid[y][x]= "+"
return False
print("false")
# Mark path as traversed
grid[y][x] = "X"
# If not at left end -- to avoid boundary issues
if x != 0:
# If not at top end
if y != 0:
left = freeMouse (x-1, y)
up = freeMouse(x, y -1)
# If at bottom end
if len(grid) == y + 1:
# If not at right end -- traverse right
if len(grid[y]) != x+1:
right = freeMouse (x + 1, y)
# If not at bottom end
else:
# If at right end -- traverse down
if len(grid[y]) == x+1:
down = freeMouse(x, y+1)
# If not at right end -- traverse down or right
else:
down = freeMouse(x, y+1)
right = freeMouse (x + 1, y)
# If you are at top end
else:
# If you are at right end
if len(grid[y])== x + 1:
# Traverse left or down
left = freeMouse (x-1, y)
down = freeMouse (x, y+1)
else:
# Traverse left, down, or right
left = freeMouse (x-1, y)
down = freeMouse (x, y+1)
right = freeMouse (x+1, y)
# If you are at left end
else:
# If you aren't at top end
if y != 0:
# If you are at right end
if len(grid) == y + 1:
# Try to traverse right or up
up = freeMouse(x,y-1)
right = freeMouse (x+1, y)
else:
# Try to traverse up, down, right
up = freeMouse(x, y-1)
down = freeMouse(x, y+1)
right = freeMouse(x + 1, y)
# If you are at top end
else:
# Try to traverse down or right
down = freeMouse(x, y+1)
right = freeMouse(x+1, y)
如果要求只是简单地从头到尾导航,我觉得你可以大大简化你的代码
def freeMouse (x, y):
if x < 0 or y < 0 or y+1 > len(grid) or x+1 > len(grid[y]):
return
if grid[y][x] == "O":
grid[0][0] = "S"
showPath()
elif grid[y][x] == "X":
return
elif grid[y][x] == ("|"):
grid[y][x]= "+"
return
elif grid[y][x] == ("+"):
return
grid[y][x] = "X"
freeMouse(x-1,y)
freeMouse(x+1,y)
freeMouse(x,y-1)
freeMouse(x,y+1)
return
但话又说回来,我不太确定你的要求。 如果上述解决方案对您不起作用,请告诉我