What if you could teach a computer to find its way out of any maze without getting lost?
Why Rat in a Maze Problem in DSA C?
Imagine you are in a big maze trying to find a path from the start to the exit. You try every turn by walking around, but it's easy to get lost or miss the right path.
Manually exploring every path is slow and confusing. You might go in circles or miss the exit because you don't remember where you have been. It's hard to keep track of all possible routes without a clear plan.
The Rat in a Maze problem uses a smart way to explore paths step-by-step, backtracking when hitting dead ends. This method remembers choices and tries alternatives until it finds the exit or proves none exists.
int x = 0, y = 0; while(not_exit) { if(can_move_right) x++; else if(can_move_down) y++; else break; }
bool solveMaze(int maze[N][N], int x, int y, int sol[N][N]) {
if(x == N-1 && y == N-1) {
sol[x][y] = 1;
return true;
}
if(isSafe(x, y)) {
sol[x][y] = 1;
if(solveMaze(maze, x+1, y, sol)) return true;
if(solveMaze(maze, x, y+1, sol)) return true;
if(solveMaze(maze, x-1, y, sol)) return true;
if(solveMaze(maze, x, y-1, sol)) return true;
sol[x][y] = 0;
}
return false;
}This approach lets you find a path through complex mazes automatically, saving time and avoiding confusion.
Robots navigating unknown buildings use similar logic to find safe paths without getting stuck.
Manual maze solving is slow and error-prone.
Backtracking explores all paths systematically.
It helps find a solution or prove none exists.
Practice
Rat in a Maze problem?Solution
Step 1: Understand the problem statement
The Rat in a Maze problem asks to find a path from the start (usually top-left) to the destination (usually bottom-right) in a maze represented by a grid.Step 2: Identify the goal
The goal is to find any valid path through open cells (usually marked 1) avoiding blocked cells (usually marked 0).Final Answer:
Find a path from the start to the destination in a maze -> Option CQuick Check:
Goal = Find path [OK]
- Thinking it's about counting walls
- Confusing with shortest path calculation
- Assuming maze filling is required
Solution
Step 1: Understand C array declaration syntax
In C, a 2D array must have both dimensions specified except for the first dimension if initialized immediately. Here, both dimensions are specified as 4.Step 2: Check initialization format
int maze[4][4] = {{1,0,0,0},{1,1,0,1},{0,1,0,0},{1,1,1,1}}; correctly declares a 4x4 int array with nested braces for each row.Final Answer:
int maze[4][4] = {{1,0,0,0},{1,1,0,1},{0,1,0,0},{1,1,1,1}}; -> Option AQuick Check:
Correct 2D array syntax = int maze[4][4] = {{1,0,0,0},{1,1,0,1},{0,1,0,0},{1,1,1,1}}; [OK]
- Omitting second dimension size
- Using single braces for all elements
- Declaring incomplete array sizes
int maze[4][4] = {
{1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 1}
};
int solution[4][4] = {0};
int solveMaze(int maze[4][4], int x, int y, int solution[4][4]) {
if (x == 3 && y == 3 && maze[x][y] == 1) {
solution[x][y] = 1;
return 1;
}
if (x >= 0 && x < 4 && y >= 0 && y < 4 && maze[x][y] == 1) {
solution[x][y] = 1;
if (solveMaze(maze, x + 1, y, solution)) return 1;
if (solveMaze(maze, x, y + 1, solution)) return 1;
solution[x][y] = 0;
return 0;
}
return 0;
}
int main() {
if (solveMaze(maze, 0, 0, solution)) {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", solution[i][j]);
}
printf("\n");
}
} else {
printf("No path found\n");
}
return 0;
}Solution
Step 1: Trace the recursive calls
The function tries to move right (x+1, y) first, then down (x, y+1). It marks path cells in solution matrix if maze cell is 1.Step 2: Follow the path found
Starting at (0,0), path moves down to (1,0), then right to (1,1), down to (2,1), down to (3,1), right to (3,2), right to (3,3). This path is marked with 1s in solution.Final Answer:
1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 1 -> Option DQuick Check:
Path marked in solution matrix = 1 0 0 0 1 1 0 0 0 1 0 0 0 1 1 1 [OK]
- Confusing row and column indices
- Assuming path includes blocked cells
- Ignoring backtracking step
solveMaze function and select the correct fix.int solveMaze(int maze[4][4], int x, int y, int solution[4][4]) {
if (x == 3 && y == 3 && maze[x][y] == 1) {
solution[x][y] = 1;
return 1;
}
if (x >= 0 && x < 4 && y >= 0 && y < 4 && maze[x][y] == 1) {
solution[x][y] = 1;
if (solveMaze(maze, x + 1, y, solution)) return 1;
if (solveMaze(maze, x, y + 1, solution)) return 1;
return 0;
}
return 0;
}Solution
Step 1: Identify missing backtracking
The function marks solution[x][y] = 1 but does not reset it to 0 if path fails, causing incorrect path marking.Step 2: Fix by adding backtracking
Before returning 0, set solution[x][y] = 0 to unmark the cell and allow other paths to be explored.Final Answer:
Add backtracking step: set solution[x][y] = 0 before returning 0 -> Option BQuick Check:
Backtracking needed to unmark path [OK]
- Forgetting to unmark solution cell
- Incorrect base case coordinates
- Changing recursion order without reason
Solution
Step 1: Understand requirement to find all paths
Finding all paths means exploring every possible route from start to end without stopping at first success.Step 2: Modify algorithm to explore all directions and store paths
Change recursion to continue after finding a path, store each valid path in a list or array, and backtrack properly to explore other routes.Final Answer:
Modify the function to explore all directions and store each valid path in a list -> Option AQuick Check:
All paths require full exploration [OK]
- Stopping after first path found
- Marking visited cells permanently blocking other paths
- Using BFS without path storage
Ready to prove it?
Pass the test and this pattern moves unknown
