Rat in a Maze
Keep one grid for the whole search. When the rat enters a cell, mark it. When every move from that cell has been tried, unmark it on the way out. The marks then always show exactly the current route. Trying the moves in the order D, L, R, U makes the routes come out sorted.
At every call, a cell is marked exactly when the current route stands on it. Marks are added on the way in and removed on the way out, so the grid always matches the route.
Problem
A rat starts in the top-left cell of an n × n maze. It wants to reach the bottom-right cell.
Each cell is either open (1) or a wall (0). The rat moves one cell at a time: D (down), L (left), R (right) or U (up). It can only step on open cells, and it may not step on the same cell twice in one route.
Return every route as a string of move letters, in alphabetical order. If there is no route, return an empty list. A wall at the start or at the end means there is no route.
In the examples, (row, col) counts rows and columns from 0, starting at the top-left.
maze, an n × n grid of 0s and 1s. The cell in row r and column c is maze[r][c].
A list of strings. Each string is one route, written as its moves. The list is in alphabetical order. The list is empty if there is no route.
There are two routes. Both pass through (2,1), (3,1) and (3,2). DDRDRR goes through (2,0), and DRDDRR goes through (1,1) instead.
Both first moves, D and R, lead into a wall, so the rat is stuck at the start.
Every cell is open. The rat can go down then right, or right then down.
- • The maze is n × n, with n at least 2.
- • Each cell is 0 (wall) or 1 (open).
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
The rule says a route may not step on the same cell twice. Is that a rule about one route, or about the whole search?
- 2.
After one route fails, which cells should the rat be able to use again for the next route?
- 3.
Two routes can pass through the same cell. If that cell stays blocked after the first visit, what happens to the second route?
- 4.
In which order should the four moves be tried so the routes come out alphabetically, without a sort?
- 5.
Can a route walk past the end cell and come back to it?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Each call gets its own copy of the visited grid; sort the answers at the end | O(n² · 3^(n²)) | O(n⁴) |
| Optimal | One grid for the whole search: mark a cell on entry, unmark it on exit; try D, L, R, U | O(3^(n²) + A · n²) | O(n²) |
Why does this belong to this pattern?
- “from the top-left to the bottom-right”A route from one cell to another in a grid. Each step is one move to a neighbouring cell.
- “return every route”We need every choice explored, not a yes or no. Each finished route is one answer.
- “cannot step on a cell twice”The rule is about one route. Many routes can use the same cell, one after another.
- “in alphabetical order”The routes are strings of letters. The order the moves are tried decides the order of the answers.
The mark belongs to the route, not to the search. Mark the cell on the way in, and unmark it on the way out, so each route sees exactly the cells it has used. This differs from the graph-traversal habit of one permanent visited set, which hides every route that shares a cell. Word Search uses the same mark and unmark on a letter board, but it stops at the first match.
Don't make this mistake
In graph traversal a visited node is never visited again. Here a cell can be used by many routes. Trace the example: the first route DDRDRR marks (2,1), (3,1) and (3,2). The search also goes up to (1,1), finds a dead end, and never clears (1,1). When it returns to (1,0) and tries R, the cell (1,1) is still marked, so the second route DRDDRR is lost. The output is only ["DDRDRR"].
Trying U, R, D, L first finds DRDDRR before DDRDRR. If the answers are returned as they are, the output is ["DRDDRR", "DDRDRR"], which is the wrong order. The routes only come out sorted when the moves are tried in alphabetical order, or when the answers are sorted.
On the maze [[0, 1], [1, 1]] the start is a wall. The search would still enter (0,0), move D to (1,0), then R to (1,1), and return ["DR"]. The correct answer is an empty list, because the rat cannot start in a wall.
Want to implement this?
Write it yourself on GeeksforGeeks, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Backtracking translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.