Recursive Backtracking Maze Generator & Solver
Build a C program that creates random mazes and solves them, drawing both as text in the terminal. Mazes are generated with recursive backtracking (a randomized depth-first search that carves passages until every cell is reachable, so there's always exactly one path between any two cells), and solved with breadth-first search, which finds the shortest path from the entrance to the exit. You can watch the solver explore the maze step by step. You'll learn representing a grid and its walls in C, recursion (and turning it into an explicit stack for large mazes), queues for breadth-first search, random numbers with a seed, dynamic memory, and drawing in the terminal.
- Estimate
- ~9.5h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
stdio.h · stdlib.h
Project roadmap
- 01
Represent the maze
~1.5hRepresent the maze as a width × height grid of cells, each storing which of its four walls are present as bit flags, allocated with malloc based on the size given on the command line (with limits, such as 2 to 100). Start with all walls present. Write helper functions to get a cell, check bounds, and remove the wall between two neighboring cells (on both sides). Done when: a grid is created and freed without leaks.
- 02
Generate with recursive backtracking
~2hStarting from a cell, mark it visited, then visit its unvisited neighbors in random order (shuffled with rand seeded by a --seed option or the time), removing the wall to each before recursing. For large mazes, replace recursion with an explicit stack array to avoid stack overflow. Done when: every cell is reachable, and the same seed always gives the same maze.
- 03
Draw the maze
~1.5hDraw the maze with + for corners, --- for horizontal walls, | for vertical walls and spaces for passages, with the entrance at the top left and the exit at the bottom right opened. Done when: the printed maze looks correct and has an opening at the entrance and exit.
- 04
Solve with breadth-first search
~2hImplement BFS with a queue (a circular array of cell indexes): start at the entrance, visit each reachable neighbor (where there's no wall) once, remembering the cell it was reached from, until the exit is found. Then follow the parents back to build the shortest path. Done when: the solver finds the path, and its length is the shortest possible.
- 05
Show and animate the solution
~1.5hMark the path on the drawing (for example with dots or a color using ANSI codes), and with --animate redraw the maze as the search explores cells, pausing briefly between steps (with nanosleep) and moving the cursor to the top instead of scrolling. Done when: the solution path is shown clearly, and the animation plays smoothly.
- 06
Test it
~1hWrite tests with assert: every cell is reachable after generation (checked with a BFS from the entrance), the number of removed walls is exactly cells minus one (a perfect maze), the same seed gives the same maze, and the solved path is valid (each step goes through an open wall). Run under valgrind. Done when: the tests pass with no memory errors.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack