Lab2 Simple Maze Solver
Lab2 Simple Maze Solver
Objective
Learning Outcomes
Learning Outcomes:
- Represent a problem space as a grid
- Implement Depth First Search (DFS) or Breadth First Search (BFS) without recursion
- Use stack/queue and visited arrays manually
- Visualize a solution path through the maze
Maze Format
Maze Format:
The maze is a 2D grid with characters:
Symbol | Meaning
---------|------------------
S | Start
E | End
# | Wall (blocked)
'' | Path (free)
Example:
maze = [
list("########"),
list("#S # #"),
list("# ## #"),
list("# ## ###"),
list("# E#"),
list("########")
]
Lab Instructions
Instructions (Step-by-Step):
1. Load the maze as a 2D list.
2. Identify the start (S) and end (E) positions.
3. Prepare a visited matrix and a stack (DFS) or queue (BFS).
4. Implement get_neighbors() to get up/down/left/right valid cells.
Lab 2: Simple Maze Solver using DFS or BFS
# Prepare structures
rows, cols = len(maze), len(maze[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
stack = []
parent = {}
# DFS traversal
while stack:
current = stack.pop()
if current == end:
break
for neighbor in get_neighbors(*current):
if not visited[neighbor[0]][neighbor[1]]:
visited[neighbor[0]][neighbor[1]] = True
parent[neighbor] = current
Lab 2: Simple Maze Solver using DFS or BFS
stack.append(neighbor)
# Print maze
for row in maze:
print("".join(row))
Report Format:
- Name:
- Roll No:
- Algorithm used (DFS/BFS):
- Difficulties faced:
- What you learned:
- Screenshot of output: