C Program for Rat in a Maze - Backtracking-2?

Rat in a maze is a popular problem that utilizes backtracking algorithm. In this problem, we have a 2D matrix representing a maze where some cells are blocked and we need to find a path from source to destination.

A maze is a 2D matrix in which some cells are blocked. One of the cells is the source cell, from where we have to start, and another is the destination where we have to reach. We must find a path from source to destination without moving into any blocked cells.

Input Maze: Solution Path: S = Start, E = End Blue = Solution Path S E S E

Syntax

int solveMaze(int row, int col);
void printSolution();

Algorithm Steps

The backtracking algorithm follows these steps −

  • Base Case: Check if current cell is the destination. If yes, puzzle is solved.
  • Valid Move Check: Verify if the cell is within bounds, unblocked, and unvisited.
  • Recursive Exploration: Try moving in all four directions (down, right, up, left).
  • Backtrack: If no direction leads to solution, mark cell as unvisited and return false.

Example Implementation

Here's the complete C program to solve the rat in a maze problem using backtracking −

#include <stdio.h>
#define SIZE 5

// The maze matrix (0 = open, 1 = blocked)
int maze[SIZE][SIZE] = {
    {0, 1, 0, 1, 1},
    {0, 0, 0, 0, 0},
    {1, 0, 1, 0, 1},
    {0, 0, 1, 0, 0},
    {1, 0, 0, 1, 0}
};

// Solution matrix to store the path
int solution[SIZE][SIZE];

// Function to print the solution matrix
void printSolution() {
    printf("Solution Path:
"); for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { printf("%d ", solution[i][j]); } printf("
"); } } // Function to solve maze using backtracking int solveMaze(int r, int c) { // Base case: if destination is reached if(r == SIZE-1 && c == SIZE-1) { solution[r][c] = 1; return 1; } // Check if current cell is valid to visit if(r >= 0 && c >= 0 && r < SIZE && c < SIZE && solution[r][c] == 0 && maze[r][c] == 0) { // Mark current cell as part of solution solution[r][c] = 1; // Try moving down if(solveMaze(r + 1, c)) return 1; // Try moving right if(solveMaze(r, c + 1)) return 1; // Try moving up if(solveMaze(r - 1, c)) return 1; // Try moving left if(solveMaze(r, c - 1)) return 1; // Backtrack: unmark current cell solution[r][c] = 0; return 0; } return 0; } int main() { // Initialize solution matrix for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { solution[i][j] = 0; } } // Solve maze starting from (0,0) if(solveMaze(0, 0)) { printSolution(); } else { printf("No solution exists
"); } return 0; }
Solution Path:
1 0 0 0 0
1 1 1 1 0
0 0 0 1 0
0 0 0 1 1
0 0 0 0 1

How It Works

The algorithm uses two matrices −

  • Maze Matrix: Represents the maze where 0 = open cell, 1 = blocked cell
  • Solution Matrix: Tracks the path where 1 = part of solution path, 0 = not in path
Step Action Description
1 Start at (0,0) Begin from top-left corner
2 Check validity Ensure cell is within bounds, unblocked, unvisited
3 Mark cell Add current cell to solution path
4 Try directions Recursively explore down, right, up, left
5 Backtrack If no direction works, unmark cell and return

Key Points

  • Time Complexity: O(4^(m×n)) in worst case where m×n is maze size
  • Space Complexity: O(m×n) for the solution matrix and recursion stack
  • The algorithm explores all possible paths systematically
  • Backtracking ensures we don't get stuck in dead ends

Conclusion

The rat in a maze problem demonstrates the power of backtracking in solving constraint satisfaction problems. By systematically exploring paths and backtracking when necessary, we can find a valid route from source to destination in any solvable maze.

Updated on: 2026-03-15T11:43:20+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements