Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Solve the maze using 'breadth-first' [Python]. Fill in the parts of the code below where it says YOU FILL THIS IN . Here is an
Solve the maze using 'breadth-first' [Python]. Fill in the parts of the code below where it says "YOU FILL THIS IN". Here is an image that gives more context if needed: https://i.imgur.com/iWfJXdy.png Here is the code:
import time class Maze(): """A pathfinding problem.""" def __init__(self, grid, location): """Instances differ by their current agent locations.""" self.grid = grid self.location = location def display(self): """Print the maze, marking the current agent location.""" for r in range(len(self.grid)): for c in range(len(self.grid[r])): if (r, c) == self.location: print('\033[96m*\x1b[0m', end=' ') # print a blue * else: print(self.grid[r][c], end=' ') # prints a space or wall print() print() def moves(self): """Return a list of possible moves given the current agent location.""" # YOU FILL THIS IN def neighbor(self, move): """Return another Maze instance with a move made.""" # YOU FILL THIS IN class Agent(): """Knows how to find the exit to a maze with BFS.""" def bfs(self, maze, goal): """Return an ordered list of moves to get the maze to match the goal.""" # YOU FILL THIS IN def main(): """Create a maze, solve it with BFS, and console-animate.""" grid = ["XXXXXXXXXXXXXXXXXXXX", "X X X X", "X XXXXX XXXX XXX XXX", "X X X X X", "X X XXX XXXXXX X X X", "X X X X X X", "X XXX XXXXXX XXXXX X", "X XXX X X X X", "X XXX XXXXX", "XXXXX XXXXXX X", "X XXX X X X X X", "XXX XXX X X XXXX X X", "X X X XX X X X", "XXXXX XXXX X XXX", "X X XXX X X", "X XXXXX X XXXX XXX X", "X X X X X X", "X X XXXXXX X XXXXX X", "X X X", "XXXXXXXXXXXXXXXXXX X"] maze = Maze(grid, (1, 1)) maze.display() agent = Agent() goal = Maze(grid, (19, 18)) path = agent.bfs(maze, goal) while path: move = path.pop(0) maze = maze.neighbor(move) time.sleep(0.50) maze.display() if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started