Rat in a Maze

medium

Given a square binary grid mat of size n x n, a rat starts at the top-left cell (0, 0) and wants to reach the bottom-right cell (n - 1, n - 1).

  • A cell with value 1 is open — the rat may stand on it.
  • A cell with value 0 is blocked — the rat may never enter it.

The rat moves one step at a time in the four directions up (U), down (D), left (L), and right (R). It may travel only through open cells, and may not visit the same cell twice within a single path.

Return all valid paths from the source to the destination. Each path is the string of moves the rat takes to get there. If no path exists, return an empty list.

Hints

You're not looking for one path — you're looking for every path. What technique explores all the choices and cleanly undoes the ones that fail?
As you walk a trail, mark each cell so you don't loop back onto yourself; the instant you back out of a cell, unmark it so a future path can still use it.
Try the four directions in the order D, L, R, U. Because that's alphabetical, the paths you finish come out already sorted — no final sort needed.

Common doubts

It means no simple path of open cells connects the top-left corner to the bottom-right — for example when the destination cell is blocked. In that case return an empty list.
Each path must be simple. Revisiting would create loops and infinitely many 'paths', so we mark cells on the current trail and skip any that are already marked.
Yes. A cell blocked in one path may be essential in another. Marking on enter and unmarking on leave keeps each path independent while sharing a single grid.

Interview follow-ups

Run a plain DFS or BFS for reachability and return as soon as the destination is reached — that turns it into an O(n^2) search instead of enumerating every route.
Extend the direction list with the new offsets and move characters. The mark/unmark backbone stays identical; only the neighbour-generation step changes.
Switch from backtracking to BFS, which explores by distance and finds a shortest route first — backtracking enumerates all paths and isn't built for shortest-only queries.

Fun facts

  • The 'mark on enter, unmark on leave' rhythm is the same trick that powers N-Queens, Sudoku solvers, and word-search grids — learn it once and reuse it everywhere.
  • Counting (not even listing) the self-avoiding paths from corner to corner of a grid is a famously hard problem with no known simple formula — the counts explode so fast they fill their own combinatorics tables.

Asked at

AmazonMicrosoftGoogleAdobeFlipkart
Frequently Sometimes Occasionally
Example 1
Input: mat = [[1,0,0,0],[1,1,0,1],[1,1,0,0],[0,1,1,1]]
Output: ["DDRDRR", "DRDDRR"]
Two trails reach the bottom-right corner: down-down-right-down-right-right and down-right-down-down-right-right.
Example 2
Input: mat = [[1,0],[1,0]]
Output: []
The destination cell (1,1) is blocked, so no path can reach it.
Constraints

- 2 <= n <= 5 - mat[i][j] is either 0 or 1 - mat is a square grid of size n x n

Solve this problem →