Skip to content

BushraTayyab/maze-solver

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🏰 Maze Solver (BFS Algorithm)

Finds the shortest path in a 2D maze using Breadth-First Search (BFS), with Matplotlib visualization.

*Developed after studying search algorithms in Harvard’s CS50 AI course.

![Maze Solution Example] image

πŸš€ Features

  • Solves mazes with custom obstacles (0 = path, 1 = wall)
  • Visualizes the optimal path using Matplotlib
  • Clean, well-commented Python code

πŸ› οΈ How to Run

python maze_solver.py

πŸ“¦ Requirements

pip install matplotlib

πŸ“ Code Overview

# Maze Solver (BFS)
from collections import deque
import matplotlib.pyplot as plt

def solve_maze(grid, start, end):
    queue = deque([[start]])
    visited = set([start])
    while queue:
        path = queue.popleft()
        x, y = path[-1]
        if (x, y) == end:
            return path
        for dx, dy in [(0,1), (1,0), (-1,0), (0,-1)]:  # Moves that could me made
            nx, ny = x+dx, y+dy
            if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]) and grid[nx][ny] != "WALL" and (nx, ny) not in visited:
                queue.append(path + [(nx, ny)])
                visited.add((nx, ny))
    return None

# Example maze (0 = empty, "WALL" = obstacle)
grid = [
    [0, 0, 0, 0],
    ["WALL", "WALL", 0, "WALL"],
    [0, 0, 0, 0]
]
path = solve_maze(grid, (0, 0), (2, 3))
print("Path:", path)

# Visualization
plt.imshow([[0 if cell == 0 else 1 for cell in row] for row in grid], cmap='binary')
plt.plot([y for x, y in path], [x for x, y in path], 'r-')
plt.show()

🀝 Contributions

Ideas for improvements? Open an issue or PR!

Inspired by Harvard's CS50 AI course

About

A Python maze solver using BFS algorithm with visualization. Finds the shortest path in a 2D grid with obstacles.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages