# Count all possible paths from source to destination in given 3D array

• Last Updated : 24 Jan, 2022

Given three integers M, N and K, the task is to count all the possible paths from the cell (0, 0, 0) to cell (M-1, N-1, K-1) in a matrix of size (M, N, K).  Movement is allowed only in three directions, which are along the positive direction of the three axes i.e. from any cell (i, j, k) movement is allowed to cells (i+1, j, k), (i, j+1, k) and (i, j, k+1).

Examples:

Input: M = 1, N = 1, K = 1
Output: 1
Explanation: As the source and destination both are same, there is only one possible way to stay there.

Input: M = 2, N = 2, K = 2
Output: 6
Explanation: The possible paths are :

• (0, 0, 0) -> (0, 1, 0) -> (0, 1, 1) -> (1, 1, 1)
• (0, 0, 0) -> (0, 1, 0) -> (1, 1, 0) -> (1, 1, 1)
• (0, 0, 0) -> (1, 0, 0) -> (1, 1, 0) -> (1, 1, 1)
• (0, 0, 0) -> (1, 0, 0) -> (1, 0, 1) -> (1, 1, 1)
• (0, 0, 0) -> (0, 0, 1) -> (0, 1, 1) -> (1, 1, 1)
• (0, 0, 0) -> (0, 0, 1) -> (1, 0, 1) -> (1, 1, 1)

Approach: This problem can be solved by using dynamic programming. Follow the below steps, to solve this problem:

• Create a 3D array of size M*N*K, say dp and initialise dp to 1.
• Use three nested loop where the outermost iterates from i = 0 to i < M, the innermost iterates from k = 0 to k < K and the middle one from j = 0 to j < N.
• Here, dp[i][j][k] indicates the number of paths from cell (0, 0, 0) to the cell (i, j, k).
• So, any cell(i, j, k), can be reached from only 3 other cells which are (i-1, j, k), (i, j-1, k) and (i, j, k-1).
• Therefore, the relation among the cells here is:

dp[i][j][k] = dp[i – 1][j][k] + dp[i][j – 1][k] + dp[i][j][k – 1]

• Fill the whole dp matrix using the above relation and then return dp[M-1][N-1][K-1] as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ code for the above approach` `#include ``using` `namespace` `std;` `// Function to find to the number of ways``// from the cell(0, 0, 0)``// to cell(M-1, N-1, K-1)``int` `numberOfWays(``int` `M, ``int` `N, ``int` `K)``{``    ``vector > > dp(``        ``M, vector >(``               ``N, vector<``int``>(K)));` `    ``// Initialising dp``    ``dp = 1;` `    ``for` `(``int` `i = 0; i < M; ++i) {``        ``for` `(``int` `j = 0; j < N; ++j) {``            ``for` `(``int` `k = 0; k < K; ++k) {``                ``if` `(i == 0 and j == 0``                    ``and k == 0) {``                    ``continue``;``                ``}``                ``dp[i][j][k] = 0;``                ``if` `(i - 1 >= 0) {``                    ``dp[i][j][k] += dp[i - 1][j][k];``                ``}``                ``if` `(j - 1 >= 0) {``                    ``dp[i][j][k] += dp[i][j - 1][k];``                ``}``                ``if` `(k - 1 >= 0) {``                    ``dp[i][j][k] += dp[i][j][k - 1];``                ``}``            ``}``        ``}``    ``}` `    ``return` `dp[M - 1][N - 1][K - 1];``}` `// Driver Code``int` `main()``{``    ``int` `M = 2, N = 2, K = 2;``    ``cout << numberOfWays(M, N, K);``    ``return` `0;``}`

## Java

 `/*package whatever //do not write package name here */``import` `java.io.*;` `class` `GFG {` `  ``// Function to find to the number of ways``  ``// from the cell(0, 0, 0)``  ``// to cell(M-1, N-1, K-1)``  ``static` `int` `numberOfWays(``int` `M, ``int` `N, ``int` `K)``  ``{``    ``int` `[][][] dp = ``new` `int``[M][N][K];`  `    ``// Initialising dp``    ``dp[``0``][``0``][``0``] = ``1``;` `    ``for` `(``int` `i = ``0``; i < M; ++i) {``      ``for` `(``int` `j = ``0``; j < N; ++j) {``        ``for` `(``int` `k = ``0``; k < K; ++k) {``          ``if` `(i == ``0` `&& j == ``0``              ``&& k == ``0``) {``            ``continue``;``          ``}``          ``dp[i][j][k] = ``0``;``          ``if` `(i - ``1` `>= ``0``) {``            ``dp[i][j][k] += dp[i - ``1``][j][k];``          ``}``          ``if` `(j - ``1` `>= ``0``) {``            ``dp[i][j][k] += dp[i][j - ``1``][k];``          ``}``          ``if` `(k - ``1` `>= ``0``) {``            ``dp[i][j][k] += dp[i][j][k - ``1``];``          ``}``        ``}``      ``}``    ``}` `    ``return` `dp[M - ``1``][N - ``1``][K - ``1``];``  ``}` `  ``// Driver Code``  ``public` `static` `void` `main (String[] args)``  ``{``    ``int` `M = ``2``, N = ``2``, K = ``2``;``    ``System.out.println(numberOfWays(M, N, K));``  ``}``}` `// This code is contributed by Potta Lokesh`

## Python3

 `# Python code for the above approach` `# Function to find to the number of ways``# from the cell(0, 0, 0)``# to cell(M-1, N-1, K-1)``def` `numberOfWays(M, N, K):``    ``dp ``=` `[[[``0` `for` `i ``in` `range``(K)] ``for` `j ``in` `range``(N)] ``for` `k ``in` `range``(M)]``    ` `    ``# Initialising dp``    ``dp[``0``][``0``][``0``] ``=` `1``;` `    ``for` `i ``in` `range``(M):``        ``for` `j ``in` `range``(N):``            ``for` `k ``in` `range``(K):``                ``if` `(i ``=``=` `0` `and` `j ``=``=` `0` `and` `k ``=``=` `0``):``                    ``continue``;``                ``dp[i][j][k] ``=` `0``;``                ``if` `(i ``-` `1` `>``=` `0``):``                    ``dp[i][j][k] ``+``=` `dp[i ``-` `1``][j][k];``                ``if` `(j ``-` `1` `>``=` `0``):``                    ``dp[i][j][k] ``+``=` `dp[i][j ``-` `1``][k];``                ``if` `(k ``-` `1` `>``=` `0``):``                    ``dp[i][j][k] ``+``=` `dp[i][j][k ``-` `1``];``    ``return` `dp[M ``-` `1``][N ``-` `1``][K ``-` `1``];` `# Driver Code` `M ``=` `2``N ``=` `2``K ``=` `2``;``print``(numberOfWays(M, N, K));` `# This code is contributed by gfgking`

## C#

 `/*package whatever //do not write package name here */``using` `System;` `class` `GFG {` `  ``// Function to find to the number of ways``  ``// from the cell(0, 0, 0)``  ``// to cell(M-1, N-1, K-1)``  ``static` `int` `numberOfWays(``int` `M, ``int` `N, ``int` `K)``  ``{``    ``int``[, , ] dp = ``new` `int``[M, N, K];` `    ``// Initialising dp``    ``dp[0, 0, 0] = 1;` `    ``for` `(``int` `i = 0; i < M; ++i) {``      ``for` `(``int` `j = 0; j < N; ++j) {``        ``for` `(``int` `k = 0; k < K; ++k) {``          ``if` `(i == 0 && j == 0 && k == 0) {``            ``continue``;``          ``}``          ``dp[i, j, k] = 0;``          ``if` `(i - 1 >= 0) {``            ``dp[i, j, k] += dp[i - 1, j, k];``          ``}``          ``if` `(j - 1 >= 0) {``            ``dp[i, j, k] += dp[i, j - 1, k];``          ``}``          ``if` `(k - 1 >= 0) {``            ``dp[i, j, k] += dp[i, j, k - 1];``          ``}``        ``}``      ``}``    ``}` `    ``return` `dp[M - 1, N - 1, K - 1];``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(``string``[] args)``  ``{``    ``int` `M = 2, N = 2, K = 2;``    ``Console.WriteLine(numberOfWays(M, N, K));``  ``}``}` `// This code is contributed by ukasp.`

## Javascript

 ``

Output
`6`

Time Complexity: O(M*N*K)
Space Complexity: O(M*N*K)

My Personal Notes arrow_drop_up