Related Articles
Count of ways to traverse a Matrix and return to origin in K steps
• Difficulty Level : Hard
• Last Updated : 06 Aug, 2020

Given three integers N, M and K, where N and M are the dimensions of the matrix and K is the maximum possible steps, the task is to count the number ways to start from (0, 0) and return traversing the matrix by using K steps only.
Note: In each step, one can move up, down, left, right, or stay at the current position. The answer could be large, so print the answer modulo 109 + 7
Examples:

Input: N = 2, M = 2, K = 2
Output:
Explanation:
Three ways are:
1)Stay, Stay.
2)Up, Down
3)Right, Left

Input: N = 3, M = 3, K = 4
Output: 23

Approach: The problem can also be solved using Memoization technique. Follow the steps below to solve the problem:

• Starting from position (0, 0), recursively call every possible position and decrease the value of steps by 1.
• Create a 3D array of size N * M * K ( DP[N][M][S]
```Possible ways for each step
can be calculated by:

DP[i][j][k]
=  Recursion(i+1, j, k-1)  +
Recursion(i-1, j, k-1)  +
Recursion(i, j-1, k-1)  +
Recursion(i, j+1, k-1)  +
Recursion(i, j, k-1).

With base condition returning 0,
whenever
i >= l or i = b or j < 0 or k < 0.
```

Below is the implementation of the above approach:

## C++

 `// C++ program to count total ` `// number of ways to return ` `// to origin after completing ` `// given number of steps. ` ` `  `#include ` `using` `namespace` `std; ` `#define MOD 1000000007 ` ` `  `long` `long` `dp; ` `int` `N, M, K; ` ` `  `// Function Initialize dp[][][] ` `// array with -1 ` `void` `Initialize() ` `{ ` ` `  `    ``for` `(``int` `i = 0; i <= 100; i++) ` `        ``for` `(``int` `j = 0; j <= 100; j++) ` `            ``for` `(``int` `z = 0; z <= 100; z++) ` `                ``dp[i][j][z] = -1; ` `} ` ` `  `// Function returns the total count ` `int` `CountWays(``int` `i, ``int` `j, ``int` `k) ` `{ ` `    ``if` `(i >= N || i < 0 ` `        ``|| j >= M || j < 0 || k < 0) ` `        ``return` `0; ` ` `  `    ``if` `(i == 0 && j == 0 ` `        ``&& k == 0) ` `        ``return` `1; ` ` `  `    ``if` `(dp[i][j][k] != -1) ` `        ``return` `dp[i][j][k]; ` `    ``else` `        ``dp[i][j][k] ` `            ``= (CountWays(i + 1, j, k - 1) % MOD ` `               ``+ CountWays(i - 1, j, k - 1) % MOD ` `               ``+ CountWays(i, j - 1, k - 1) % MOD ` `               ``+ CountWays(i, j + 1, k - 1) % MOD ` `               ``+ CountWays(i, j, k - 1) % MOD) ` `              ``% MOD; ` ` `  `    ``return` `dp[i][j][k]; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``N = 3; ` `    ``M = 3; ` `    ``K = 4; ` ` `  `    ``Initialize(); ` `    ``cout << CountWays(0, 0, K) ` `         ``<< ``"\n"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count total  ` `// number of ways to return  ` `// to origin after completing  ` `// given number of steps.  ` `class` `GFG{ ` `     `  `static` `int` `[][][] dp = ``new` `int``[``101``][``101``][``101``];  ` `static` `int` `N, M, K; ` `static` `int` `MOD = ``1000000007``;  ` `     `  `// Function Initialize dp[][][]  ` `// array with -1  ` `public` `static` `void` `Initialize()  ` `{  ` `    ``for``(``int` `i = ``0``; i <= ``100``; i++)  ` `       ``for``(``int` `j = ``0``; j <= ``100``; j++)  ` `          ``for``(``int` `z = ``0``; z <= ``100``; z++)  ` `             ``dp[i][j][z] = -``1``;  ` `}  ` `     `  `// Function returns the total count  ` `public` `static` `int` `CountWays(``int` `i, ``int` `j, ``int` `k)  ` `{  ` `    ``if` `(i >= N || i < ``0` `|| ` `        ``j >= M || j < ``0` `|| k < ``0``)  ` `        ``return` `0``;  ` `     `  `    ``if` `(i == ``0` `&& j == ``0` `&& k == ``0``)  ` `        ``return` `1``;  ` `     `  `    ``if` `(dp[i][j][k] != -``1``)  ` `        ``return` `dp[i][j][k];  ` `     `  `    ``else` `        ``dp[i][j][k] = (CountWays(i + ``1``, j, k - ``1``) % MOD + ` `                       ``CountWays(i - ``1``, j, k - ``1``) % MOD + ` `                       ``CountWays(i, j - ``1``, k - ``1``) % MOD + ` `                       ``CountWays(i, j + ``1``, k - ``1``) % MOD + ` `                       ``CountWays(i, j, k - ``1``) % MOD) % MOD;  ` `     `  `    ``return` `dp[i][j][k];  ` `}  ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``N = ``3``;  ` `    ``M = ``3``;  ` `    ``K = ``4``;  ` `     `  `    ``Initialize();  ` `    ``System.out.println(CountWays(``0``, ``0``, K)); ` `}  ` `}  ` ` `  `// This code is contributed by grand_master `

## Python3

 `# Python3 program to count total ` `# number of ways to return ` `# to origin after completing ` `# given number of steps. ` `MOD ``=` `1000000007` ` `  `dp ``=` `[[[``0` `for` `i ``in` `range``(``101``)]  ` `          ``for` `i ``in` `range``(``101``)]  ` `          ``for` `i ``in` `range``(``101``)] ` `N, M, K ``=` `0``, ``0``, ``0` ` `  `# Function Initialize dp[][][] ` `# array with -1 ` `def` `Initialize(): ` ` `  `    ``for` `i ``in` `range``(``101``): ` `        ``for` `j ``in` `range``(``101``): ` `            ``for` `z ``in` `range``(``101``): ` `                ``dp[i][j][z] ``=` `-``1` ` `  `# Function returns the total count ` `def` `CountWays(i, j, k): ` ` `  `    ``if` `(i >``=` `N ``or` `i < ``0` `or` `        ``j >``=` `M ``or` `j < ``0` `or` `k < ``0``): ` `        ``return` `0` ` `  `    ``if` `(i ``=``=` `0` `and` `j ``=``=` `0` `and` `k ``=``=` `0``): ` `        ``return` `1` ` `  `    ``if` `(dp[i][j][k] !``=` `-``1``): ` `        ``return` `dp[i][j][k] ` `    ``else``: ` `        ``dp[i][j][k] ``=` `(CountWays(i ``+` `1``, j, k ``-` `1``) ``%` `MOD ``+` `                       ``CountWays(i ``-` `1``, j, k ``-` `1``) ``%` `MOD ``+` `                       ``CountWays(i, j ``-` `1``, k ``-` `1``) ``%` `MOD ``+` `                       ``CountWays(i, j ``+` `1``, k ``-` `1``) ``%` `MOD ``+` `                       ``CountWays(i, j, k ``-` `1``) ``%` `MOD) ``%` `MOD ` `    ``return` `dp[i][j][k] ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``N ``=` `3` `    ``M ``=` `3` `    ``K ``=` `4` ` `  `    ``Initialize() ` `    ``print``(CountWays(``0``, ``0``, K)) ` ` `  `# This code is contributed by mohit kumar 29 `

## C#

 `// C# program to count total  ` `// number of ways to return  ` `// to origin after completing  ` `// given number of steps.  ` `using` `System; ` ` `  `class` `GFG{  ` `     `  `static` `int` `[,,] dp = ``new` `int``[101, 101, 101];  ` `static` `int` `N, M, K;  ` `static` `int` `MOD = 1000000007;  ` `     `  `// Function Initialize [,]dp[]  ` `// array with -1  ` `public` `static` `void` `Initialize()  ` `{  ` `    ``for``(``int` `i = 0; i <= 100; i++)  ` `        ``for``(``int` `j = 0; j <= 100; j++)  ` `            ``for``(``int` `z = 0; z <= 100; z++)  ` `                ``dp[i, j, z] = -1;  ` `}  ` `     `  `// Function returns the total count  ` `public` `static` `int` `CountWays(``int` `i, ``int` `j, ``int` `k)  ` `{  ` `    ``if` `(i >= N || i < 0 ||  ` `        ``j >= M || j < 0 || k < 0)  ` `        ``return` `0;  ` `     `  `    ``if` `(i == 0 && j == 0 && k == 0)  ` `        ``return` `1;  ` `     `  `    ``if` `(dp[i, j, k] != -1)  ` `        ``return` `dp[i, j, k];  ` `     `  `    ``else` `        ``dp[i, j, k] = (CountWays(i + 1, j, k - 1) % MOD +  ` `                       ``CountWays(i - 1, j, k - 1) % MOD +  ` `                       ``CountWays(i, j - 1, k - 1) % MOD +  ` `                       ``CountWays(i, j + 1, k - 1) % MOD +  ` `                       ``CountWays(i, j, k - 1) % MOD) % MOD;  ` `     `  `    ``return` `dp[i, j, k];  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``N = 3;  ` `    ``M = 3;  ` `    ``K = 4;  ` `     `  `    ``Initialize();  ` `    ``Console.WriteLine(CountWays(0, 0, K));  ` `}  ` `}  ` ` `  `// This code is contributed by Amit Katiyar `

Output:

```23
``` My Personal Notes arrow_drop_up
Recommended Articles
Page :