The Wayback Machine - https://web.archive.org/web/20220117133121/https://www.geeksforgeeks.org/count-all-possible-paths-from-source-to-destination-in-given-3d-array/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 17 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[0][0][0] 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 <bits/stdc++.h>
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<vector<vector<int> > > dp(
        M, vector<vector<int> >(
               N, vector<int>(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 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;
}
Output
6

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!