Maze Solving in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Finding a Path with Backtracking

A maze is a grid where 1 is a wall and 0 is open. Backtracking tries a direction; if the recursive search from the new cell fails, it unmarks the cell and tries the next direction. A separate seen array stops the search from revisiting cells. When the goal cell is reached the recursion returns success all the way up, leaving the path marked.

Remember
solve(r, c):
    if out of bounds, wall, or seen: return 0
    mark seen and path
    if goal: return 1
    for each direction: if solve(next) return 1
    unmark path; return 0           /* dead end */

Cheat Sheet

Keep these facts handy while you work with Maze Solving in C in C.

TopicDetail
DirectionsRight, down, left, up
SeenPrevents cycles
PathMarked while exploring, unmarked on dead ends
ResultFirst path found, not necessarily shortest

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: N-Queens Problem in C.

Example 1 — Solve a 4 by 4 maze

# is a wall and * marks the path from the top-left to the bottom-right.

C
#include <stdio.h>
#define N 4

static const int maze[N][N] = {
    {0, 0, 0, 1},
    {1, 0, 1, 1},
    {0, 0, 0, 0},
    {0, 1, 1, 0}
};
static int path[N][N], seen[N][N], backtracks;

static int solve(int r, int c) {
    if (r < 0 || r >= N || c < 0 || c >= N || maze[r][c] || seen[r][c]) return 0;
    seen[r][c] = 1;
    path[r][c] = 1;
    if (r == N - 1 && c == N - 1) return 1;

    static const int dr[] = {0, 1, 0, -1};
    static const int dc[] = {1, 0, -1, 0};
    for (int d = 0; d < 4; d++)
        if (solve(r + dr[d], c + dc[d])) return 1;

    path[r][c] = 0;
    backtracks++;
    return 0;
}

int main(void) {
    if (solve(0, 0)) {
        for (int r = 0; r < N; r++) {
            for (int c = 0; c < N; c++)
                printf("%c%s", maze[r][c] ? '#' : (path[r][c] ? '*' : '.'), c + 1 < N ? " " : "");
            printf("\n");
        }
    }
    printf("dead ends backtracked: %d\n", backtracks);
    return 0;
}

How It Works

  1. From the start the search goes right twice and reaches the dead end at row 0, column 2.
  2. That cell is unmarked and the search backs up to column 1, then goes down.
  3. It continues down and right to the goal; the dead-end cell is shown as . because it is no longer on the path.

Edge Cases & Pitfalls

Check these before relying on Maze Solving in C in real programs.

seen

No visited array

Open loops in a maze cause infinite recursion.

short

Assuming shortest

Depth-first search finds some path. Use BFS for the shortest path.

Key Takeaways

  • Mark, explore, unmark.
  • Visited prevents loops.
  • DFS finds a path, not the shortest.

One line: A maze is a grid where 1 is a wall and 0 is open.

Frequently Asked Questions

Use recursive depth-first search with backtracking on a grid, marking visited cells and unmarking cells that lead to dead ends.
Use breadth-first search, which explores cells in order of distance.
Continue with "N-Queens Problem in C" next. Place N queens on an N by N board with backtracking and count solutions. The C Recursion and Backtracking hub lists every topic in order.

Next: N-Queens Problem in C

Place N queens on an N by N board with backtracking and count solutions.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful