


There are two possible scenarios when a turn can occur at point (i, j):
Turns Right: (i - 1, j) -> (i, j) -> (i, j + 1)
Turns Down: (i, j-1) -> (i, j) -> (i+1, j)
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.
The first line of each test case contains three space-separated integers ‘N’, ’M’, and ‘K’ denoting the number of rows, columns, and maximum turn around respectively.
For each case, we need to print the total number of possible paths % (10 ^ 9 + 7).
The answer should be in the mod(10 ^ 9 + 7)
The output of each test case will be printed in a separate line.
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 50
1 <= M <= 50
1 <= K <= 100
Time limit: 1 sec.
Here, we will use basic recursion to calculate the number of the unique paths with ‘K’ most turns around. We can declare a function having 4 parameters as ith row index, jth row index, remaining k turns possible from that index(K), and the direction of the movement (d). For row, we consider ‘d’ equal to ‘0’ and, for column ‘d’ is equal to ‘1’. Using the recursive function we shall call the function for both adjacent cells (right cell and bottom cell), and add them up and return to the parent function. Inside our recursive function, firstly we need to have certain base conditions.
Our base conditions will into the following constraints:
Now we can compute for both the directions separately looking into the current movement pattern and return to the parent function.
For each arithmetic operation, we need to use the mod ( 10 ^ 9 + 7).
In our brute force approach, we can add memorizations and improve the time complexity. We can declare a global matrix having 4 dimensions as the 4 parameters of our recursive function. Now, in this approach, when we reach a certain cell, we will check whether the output for that cell is present in the DP table or not. If the output is present for that cell, we will return that without computing for that cell again, otherwise we will compute for the cell and store the result in the DP table for further use.
Our transitions are as follows: