Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Python program that reads from a text file named maze.txt a position (i,

ID: 3707533 • Letter: W

Question

Write a Python program that reads from a text file named maze.txt a position (i, j) in the maze and a plan of the maze then it displays all the paths leading to existing exits in the maze. See Figure 1. A maze is made of positions, consisting of walls (represented by,spaces (represented by 'S) and exits (represented by E'), where it's allowed to move through the spaces to the exit, but it's not allowed to traverse walls. Requirements: 1. The data in the file maze.txt are separated by space 2. Coordinates of the maze start from top to down(i0..M-1) and from left to right (N-0...N-1) 3. lan adequate error message should be displayed if the initial position corresponds to a wall or is outside the maze area f the initial position corresponds to an exit or a space, all the paths connecting the position (i, j) to all the exits should be displayed. 5. The file should be read either from the main() or a dedicated function readFile) called from the main() 6. Beside the main() function, you may use a non-recursive function called solveMaze(maze, i, that calls a recursive helper function named recSolveMaze(...) that returns all the possible paths to the existing exits. Hint 1. Apply the strategy discussed in the class. This may require to use a function generatePaths(..) that generates 4 extensions to the currentPath (by adding the right neighbor, the left neighbor, the neighbor in the previous row and the neighbor in the following row). See Figure 2 2. You may need to set any position in the maze that has been visited in a previous path, to 'V in order to avoid visiting it later through another path. Figure 3 shows a sample run of the program when using the data saved maze.txt shown if Figure 1 1 6 S SSSE 2E Figure 1: Sample contents of maze.txt Figure 2: The 4 allowed movements from a position (j)

Explanation / Answer

//It shows the python code whcih is not compiled but will help in designing the solution in python for the maze problem

//Below illustrated procedure which can be used to load the file

def main():

mazefile = open('Maze.txt','r+')

for line in mazefile:

line = line.strip()

print line

mazefile.close()

main()//calling the main function

//Following python code fpr solve maze can be used to search the maze path


def SolveMaze(x,y, mazeList):
# if the end of the maze is encountered then it will return true
if mazeList[x][y] == 'E'://Exit is sncountered
return True
elif mazeList[x][y] == "*"://Wall check
return False
# returns False if the path is already visited
elif mazeList[x][y] == 'a': //Already visited path check
return False
# marks path with '*'
mazeList[x][y] = '*'

# recursive search   
if ((SolveMaze(x+1, y, mazeList))
or (SolveMaze(x, y-1, mazeList))
or (SolveMaze(x-1, y, mazeList))
or (SolveMaze(x, y+1, mazeList))):
mazeList[x][y] = 'o'
return True
return False

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote