0% found this document useful (0 votes)
13 views3 pages

Lab2 Simple Maze Solver

This document outlines a lab assignment for an Introduction to Artificial Intelligence course, focusing on implementing a maze solver using Depth First Search (DFS) or Breadth First Search (BFS). It includes objectives, learning outcomes, maze format, step-by-step instructions, a Python code template, and a submission checklist. Students are expected to represent a maze as a grid, implement search algorithms, and visualize the solution path.

Uploaded by

ayesha.ahmed9009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Lab2 Simple Maze Solver

This document outlines a lab assignment for an Introduction to Artificial Intelligence course, focusing on implementing a maze solver using Depth First Search (DFS) or Breadth First Search (BFS). It includes objectives, learning outcomes, maze format, step-by-step instructions, a Python code template, and a submission checklist. Students are expected to represent a maze as a grid, implement search algorithms, and visualize the solution path.

Uploaded by

ayesha.ahmed9009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab 2: Simple Maze Solver using DFS or BFS

Objective

Course: Introduction to Artificial Intelligence (2nd Semester)


Topic: Search Algorithms (Manual DFS or BFS)
Objective:
Learn how to manually implement a maze solver using basic AI techniques (DFS or BFS) and data structures (stack or
queue).

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

5. Traverse from S to E using DFS or BFS.


6. Trace the path back using a parent dictionary.
7. Display the maze with path marked by '.'

Python Code Template

# Define the maze


maze = [
list("########"),
list("#S # #"),
list("# ## #"),
list("# ## ###"),
list("# E#"),
list("########")
]

# Get start and end


start = (1, 1)
end = (4, 6)

# Prepare structures
rows, cols = len(maze), len(maze[0])
visited = [[False for _ in range(cols)] for _ in range(rows)]
stack = []
parent = {}

# Add start to stack


stack.append(start)
visited[start[0]][start[1]] = True

# Function to get neighbors


def get_neighbors(r, c):
directions = [(-1,0), (1,0), (0,-1), (0,1)]
neighbors = []
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < rows and 0 <= nc < cols:
if maze[nr][nc] != '#' and not visited[nr][nc]:
neighbors.append((nr, nc))
return neighbors

# 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)

# Backtrack to find path


path = []
cell = end
while cell != start:
path.append(cell)
cell = parent[cell]
path.append(start)
path.reverse()

# Show path in maze


for r, c in path:
if maze[r][c] not in ('S', 'E'):
maze[r][c] = '.'

# Print maze
for row in maze:
print("".join(row))

Submission Checklist and Report Format

Lab Submission Checklist:


- [ ] Maze loaded correctly
- [ ] Manual DFS or BFS implemented
- [ ] Path tracked and backtracked correctly
- [ ] Solved maze displayed with `.` symbols
- [ ] Code is well-commented
- [ ] Short report attached

Report Format:
- Name:
- Roll No:
- Algorithm used (DFS/BFS):
- Difficulties faced:
- What you learned:
- Screenshot of output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy