#C2039. Counting Distinct Paths in a Grid with Obstacles
Counting Distinct Paths in a Grid with Obstacles
Counting Distinct Paths in a Grid with Obstacles
You are given a grid with N rows and M columns. Each cell in the grid is either empty (denoted by 0
) or contains an obstacle (denoted by 1
). A robot starts at the top-left corner, cell \((1,1)\), and must reach the bottom-right corner, cell \((N,M)\), by only moving either down or right at any step.
Your task is to determine the number of distinct paths the robot can take to reach the destination while avoiding obstacles. Note that if the starting or ending cell contains an obstacle, the answer is \(0\).
Input Constraints:
- The first line contains two integers \(N\) and \(M\) -- the number of rows and columns.
- The following \(N\) lines each contain \(M\) integers (either
0
or1
), representing the grid.
inputFormat
The input is read from standard input (stdin). The first line contains two integers \(N\) and \(M\). The next \(N\) lines each contain \(M\) integers separated by spaces, representing the grid. A value of 1
indicates an obstacle, and a value of 0
indicates a free cell.
outputFormat
Output a single integer to standard output (stdout) denoting the number of distinct paths from the top-left corner to the bottom-right corner.
## sample2 2
0 1
0 0
1
</p>