Professional Documents
Culture Documents
def solveMazeWithPath
def solveMazeWithPath
R, C = len(maze), len(maze[0])
start = (0, 0)
for r in range(R):
for c in range(C):
if maze[r][c] == 'S':
start = (r, c)
break
else:
continue
break
else:
return None
queue = deque()
queue.appendleft((start[0], start[1], 0, [start[0] * C + start[1]]))
directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]
visited = [[False] * C for _ in range(R)]
while len(queue) != 0:
coord = queue.pop()
visited[coord[0]][coord[1]] = True
if maze[coord[0]][coord[1]] == "E":
return coord[2], [[i//C, i%C] for i in coord[3]] # Return path length, boxes on path
with open("maze.txt") as f:
maze = []
for line in f:
maze.append([i for i in line.strip("\n")])
pathLen, pathItems = solveMazeWithPath(maze)
print("Length of path:", pathLen)
print("Path Items:", *pathItems)