Number of ways
Practice
3.9 (19 votes)
Algorithms
Dynamic programming
2d dynamic programming
Problem
90% Success 5055 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There is an \(N \times M\) grid in which there are \(N\) rows and \(M\) colums. A cell \((i,j)\) is defined as the \(i^{th}\) row from the top and \(j^{th}\) column from left. You are located at \((1,\ 1)\) initially and can perform the following steps any number of times:

  1. You can move any number of steps downwards.
  2. You can move any number of steps towards the right.

There are some obstacles in the path.

You are initially located at \((1,\ 1)\) and wants to reach \((N,\ M)\) but you are interested in knowing the number of ways you can reach \((N,\ M)\) from \((1,\ 1)\). Since these numbers can huge, print it modulo \((10^9+7)\).

Two ways are considered different if they have a different number of steps or differ in some positions.

Note

  1. You can never move out of the grid.
  2. You cannot ignore the conditions.
  3. The first cell \((1,\ 1)\) and the last cell \((N,\ M)\) do not contain obstacles.
  4. A free cell is denoted by . and an obstacle is denoted by *.

Input format

  • The first line contains \(T\) denoting the number of test cases.
  • The first line of each test case contains the number of rows \(N\) and the number of columns \(M\).
  • Each of the next \(N\) lines of each test case consist of a string of length \(M\).

Output format

For each test case, print a single line denoting the number of ways to reach \((N,\ M)\) from \((1,\ 1)\) modulo \((10^9+7)\).

Constraints

\(1 \leq T \leq 10\)

\(1 \leq N,M \leq 1000\)

Each cell consists of either . or *.

The grid consists of \((1,\ 1)\) and \((N,\ M)\).

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
8 votes
Tags:
AlgorithmsApprovedDynamic ProgrammingMediumTwo dimensional
Points:30
14 votes
Tags:
ApprovedDynamic ProgrammingMathMediumOpenTwo dimensional
Points:30
4 votes
Tags:
Dynamic Programming2D dynamic programmingAlgorithms