Number of ways

3.9

19 votes
Algorithms, Dynamic Programming, 2D dynamic programming
Problem

There is an grid in which there are rows and colums. A cell is defined as the  row from the top and  column from left. You are located at 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  and wants to reach but you are interested in knowing the number of ways you can reach from . Since these numbers can huge, print it modulo .

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 and the last cell do not contain obstacles.
  4. A free cell is denoted by . and an obstacle is denoted by *.

Input format

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

Output format

For each test case, print a single line denoting the number of ways to reach from modulo .

Constraints

Each cell consists of either . or *.

The grid consists of and .

Sample Input
1
3 3
...
.*.
...
Sample Output
8
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

There are 8 ways:

  1. (1,1),(1,2),(1,3),(2,3),(3,3)
  2. (1,1),(1,2),(1,3),(3,3)
  3. (1,1),(1,3),(2,3),(3,3)
  4. (1,1),(1,3),(3,3)
  5. (1,1),(2,1),(3,1),(3,2),(3,3)
  6. (1,1),(3,1),(3,2),(3,3)
  7. (1,1),(2,1),(3,1),(3,3)
  8. (1,1),(3,1),(3,3)
Editor Image

?