Rat in a Maze

Practice this coding question on Coding Gurukul.

Problem statement

A laboratory rat travels through open cells of a square maze. A researcher wants to record every route from the upper-left entrance to the lower-right exit without revisiting a cell. Your task Enumerate all paths from (0, 0) to (n - 1, n - 1), moving only between orthogonally adjacent open cells. Important details A cell containing 1 is open and 0 is blocked. Use D, L, R and U for down, left, right and up. A cell may occur at most once within a path, but different paths may share cells. Sort path strings lexicographically. A blocked entrance or exit gives zero paths. For a one-cell open maze, the single zero-move path is written EMPTY. Worked example For an open 2 by 2 maze there are two paths: DR and RD. Both reach the exit in two steps without revisiting any cell, so print 2 followed by DR and RD.

Input format

First line: n. Next n lines: n space-separated binary cell values each.

Output format

Print the path count, followed by one path per line in lexicographic order. Use EMPTY for the zero-move path.

Constraints

1 <= n <= 4; every cell is 0 or 1.

Sample input

2
1 1
1 1

Sample output

2
DR
RD

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…