A comprehensive C++ implementation of recursive maze generation algorithms featuring both iterative and recursive approaches with stack-based data structures for efficient maze generation.
This project implements a sophisticated maze generation system that creates intricate and challenging mazes using two primary algorithms:
- Iterative Depth-First Search - Stack-based implementation
- Recursive Depth-First Search - Classic recursive approach
Both algorithms ensure perfect maze generation with exactly one path between any two cells.
- Multiple Generation Algorithms: Choose between iterative and recursive implementations
- Customizable Maze Dimensions: Generate mazes from 3x3 to 50x50 cells
- Seeded Generation: Reproducible mazes using custom seeds
- Multiple Visualization Formats: Unicode box drawing and ASCII representations
- Performance Testing: Built-in benchmarking for algorithm comparison
- Maze Solving: Bonus feature with path-finding capabilities
- Interactive Menu System: User-friendly command-line interface
- C++ Compiler: GCC 7.0+ or Clang 6.0+ (C++17 support required)
- Operating System: Linux, macOS, or Windows (with appropriate compiler)
- Memory: Minimal requirements (scales with maze size)
valgrind- For memory leak detectioncppcheck- For static code analysisclang-format- For code formatting
# Clone or download the project files
# Navigate to the project directory
cd Maze
# Compile using make
make
# Run the program
make rung++ -std=c++17 -Wall -Wextra -O2 main.cpp Maze.cpp -o maze_generator
./maze_generatormake all # Build the project (default)
make debug # Build with debug information
make release # Build with full optimization
make run # Build and run the program
make clean # Remove build artifacts
make help # Show all available targetsRun the program and choose from the following options:
- Generate maze (Iterative) - Stack-based depth-first search
- Generate maze (Recursive) - Classic recursive implementation
- Generate custom size maze - Specify dimensions
- Generate maze with custom seed - Reproducible results
- Show maze in ASCII format - Alternative visualization
- Show detailed maze information - Statistics and analysis
- Solve current maze - Pathfinding demonstration
- Generate multiple mazes comparison - Side-by-side algorithm comparison
- Performance test - Benchmark different maze sizes
=== MAZE (10x10) ===
โโโโฌโโโฌโโโฌโโโฌโโโฌโโโฌโโโฌโโโฌโโโฌโโโ
โ โ โ โ
โโโโผ โโโโฌโโโฌโโโผ โโโโฌโโโผ โ
โ โ โ โ โ
โ โฌโโโค โโโโฌโโโดโโโฌโโโค โโโโค
โ โ โ โ โ โ
โโโโดโโโดโโโดโโโดโโโดโโโดโโโดโโโดโโโ
void generateMazeIterative() {
// Initialize with starting cell
Cell* currentCell = getCell(0, 0);
currentCell->visited = true;
cellStack.push(currentCell);
while (!cellStack.empty()) {
currentCell = cellStack.top();
// Get unvisited neighbors
std::vector<Cell*> neighbors = getUnvisitedNeighbors(currentCell);
if (!neighbors.empty()) {
// Choose random neighbor and carve path
Cell* chosenNeighbor = neighbors[random_index];
removeWall(currentCell, chosenNeighbor);
chosenNeighbor->visited = true;
cellStack.push(chosenNeighbor);
} else {
// Backtrack
cellStack.pop();
}
}
}void generateMazeRecursive(int x, int y) {
Cell* currentCell = getCell(x, y);
currentCell->visited = true;
// Get and shuffle neighbors for randomness
std::vector<Cell*> neighbors = getUnvisitedNeighbors(currentCell);
std::shuffle(neighbors.begin(), neighbors.end(), rng);
// Recursively visit unvisited neighbors
for (Cell* neighbor : neighbors) {
if (!neighbor->visited) {
removeWall(currentCell, neighbor);
generateMazeRecursive(neighbor->x, neighbor->y);
}
}
}- Both algorithms: O(n) where n = width ร height
- Space Complexity: O(n) for grid storage + O(n) for stack/recursion
Testing 10x10 maze:
Iterative: 156 ฮผs
Recursive: 203 ฮผs
Difference: 47 ฮผs
Testing 30x30 maze:
Iterative: 2840 ฮผs
Recursive: 3120 ฮผs
Difference: 280 ฮผs
Maze/
โโโ Maze.h # Header file with class definitions
โโโ Maze.cpp # Implementation of maze algorithms
โโโ main.cpp # Main program with user interface
โโโ Makefile # Build system configuration
โโโ README.md # This documentation
โโโ bin/ # Compiled executables (created by make)
โโโ obj/ # Object files (created by make)
Cell: Represents individual maze cells with wall and visit informationMaze: Main class containing generation algorithms and utilitiesDirection: Enumeration for navigation (TOP, RIGHT, BOTTOM, LEFT)
generateMazeIterative(): Stack-based maze generationgenerateMazeRecursive(): Recursive maze generationprintMaze(): Unicode box drawing visualizationprintMazeASCII(): ASCII character visualizationsolveMaze(): Pathfinding algorithm
// Create a 15x20 maze with specific seed
Maze customMaze(15, 20, 12345);
// Generate using recursive algorithm
customMaze.generateMazeRecursive();
// Display the result
customMaze.printMaze();The maze generation can be customized by:
- Adjusting maze dimensions (minimum 3x3, maximum limited by memory)
- Using different random seeds for reproducible results
- Modifying the random number generator for different distributions
-
Compilation Errors
# Ensure C++17 support g++ --version # Update compiler if needed
-
Stack Overflow (Large Recursive Mazes)
# Use iterative algorithm for large mazes # Or increase stack size: ulimit -s unlimited
-
Memory Issues
# Check memory usage make memcheck
- Use iterative algorithm for larger mazes (>40x40)
- Compile with
-O2or-O3for better performance - Consider maze dimensions vs. available memory
- Additional maze generation algorithms (Kruskal's, Prim's)
- Graphical user interface (GUI) version
- Maze export to image formats
- Advanced solving algorithms (A*, Dijkstra)
- 3D maze generation
- Multi-threaded generation for large mazes
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit pull requests or open issues for:
- Bug fixes
- Performance improvements
- Additional algorithms
- Documentation improvements
For questions or issues:
- Check the troubleshooting section
- Review the code comments for implementation details
- Test with different maze sizes and parameters
This project demonstrates:
- Data Structures: Stacks, 2D arrays, vectors
- Algorithms: Depth-first search, backtracking
- Design Patterns: Object-oriented programming principles
- Performance Analysis: Algorithm comparison and benchmarking
- C++ Features: Modern C++17 features, STL usage
Perfect for:
- Computer Science students studying algorithms
- Developers learning recursive problem-solving
- Anyone interested in maze generation and pathfinding algorithms
Happy Maze Generating! ๐