#K49972. Unique Paths in a Maze
Unique Paths in a Maze
Unique Paths in a Maze
You are given an N x N maze represented by a grid. Each cell of the maze contains either a .
(representing an open cell) or a #
(representing an obstacle). You need to determine the number of unique paths from the top-left corner to the bottom-right corner of the maze if you can only move either down or right at any step.
Note: The starting cell (top-left) and the destination cell (bottom-right) must be open (.
). If either is blocked by an obstacle (#
), the answer is 0.
The uniqueness of a path is determined by the sequence of moves made. In other words, two paths are considered different if they do not have exactly the same sequence of moves.
The mathematical formulation can be written in LaTeX as:
$$\text{paths}(i,j) = \begin{cases} 0, & \text{if } maze[i][j] = '#' \\ 1, & \text{if } i = N-1 \text{ and } j = N-1 \\ \text{paths}(i+1,j) + \text{paths}(i,j+1), & \text{otherwise} \end{cases} $$Your task is to implement this logic.
inputFormat
The first line of input contains an integer N
representing the size of the maze. The following N
lines each contain a string of length N
consisting only of the characters .
and #
, representing the maze's rows.
For example:
3 .#. ... #..
outputFormat
Output a single integer which is the number of unique paths from the top-left corner to the bottom-right corner using only right and down moves.
## sample2
..
..
2
</p>