我正在研究一个迷宫求解器,虽然我的所有功能都在运行,但我无法获得一部分工作。我需要用字母(a-z)的升序字母而不是单个字符来标记“解决方案路径”。
我无法让它继续迭代一个字母字符列表,我所取得的最好成绩就是停在字母'a'上,只是将所有字母都改成了字母。
迷宫:
###_###
#_____#
#_##_##
#_##__#
#_#####
'解决了迷宫':
###a###
#aaa__#
#a##_##
#a##__#
#a#####
代码:
PATH = "_"
START = "_"
VISITED = "."
SOLUTION = "o"
class Maze:
def __init__(self, ascii_maze):
# splits maze string into separate values on each new line
# uses list comp. to create 'matrix' out of maze
self.maze = [list(row) for row in ascii_maze.splitlines()]
# finds index first '_' character which denotes the beginning
self.start_y = [row.count(START) for row in self.maze].index(1)
# finds position where '_' is located within the 'y' line which returns the index position
self.start_x = self.maze[self.start_y].index(START)
# returns string representation of maze object, joining maze elements passed in as parameters
# used to print maze with 'cells' and be user friendly
def __repr__(self):
return "\n".join("".join(row) for row in self.maze)
def solve_maze(self, x=None, y=None):
# assigns starting (x,y) position
if x is None:
x, y = self.start_x, self.start_y
# checks if the coordinate is in the path/start
if self.maze[y][x] in (PATH, START):
# marks spot as 'visited' for recursion check
self.maze[y][x] = VISITED
# uses recursion to check paths by checking each direction and making a decision off that
try:
if (self.solve_maze(x+1, y) or
self.solve_maze(x-1, y) or
self.solve_maze(x, y+1) or
self.solve_maze(x, y-1)):
self.maze[y][x] = SOLUTION
return True
# this exception is what occurs when the program tries to leave the maze (aka it found the exit)
# also marks it
except IndexError:
self.maze[y][x] = SOLUTION
return True
return False
if __name__ == "__main__":
import sys
alphabet_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k','l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# for checking mazes through terminal
if len(sys.argv) > 1:
maze = Maze(open(sys.argv[1]).read())
# if none available, defaults to 'maze' text file
else:
maze = Maze(open('maze').read())
# prints string representation of maze to replace "visited" areas (. char used for testing) with original "_"
if maze.solve_maze():
# converting to string allows for easy replacement of things
maze = str(maze)
maze = maze.replace(".", "_")
for char in maze:
if char == "o":
for item in alphabet_list:
maze = maze.replace(char, item)
print(maze)
答案 0 :(得分:0)
因为您需要将解决方案字符设置为已解决(事后很难通过不知道的字符串操作重新创建解决方案字符,它实际上将重新导航整个解决方案路径):
我们将使用辅助函数将字母表中的下一个字符返回给我们,重置为' z':
SOLUTION = ord('a') - 1
def getSolutionChar():
global SOLUTION
if SOLUTION >= ord('z'):
SOLUTION = ord('a')
else:
SOLUTION += 1
return chr(SOLUTION)
替换try
块:
try:
if (self.solve_maze(x+1, y) or
self.solve_maze(x-1, y) or
self.solve_maze(x, y+1) or
self.solve_maze(x, y-1)):
self.maze[y][x] = getSolutionChar()
return True
except IndexError:
self.maze[y][x] = getSolutionChar()
return True
请勿忘记删除以下内容,因为不再需要:
alphabet_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k','l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for char in maze:
if char == "o":
for item in alphabet_list:
maze = maze.replace(char, item)