![]() |
VOOZH | about |
Given three integers p, q, and r, the task is to count all the possible paths from the cell (0, 0, 0) to cell (p-1, q-1, r-1) in a matrix of size (p, q, r). 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: p = 1, q = 1, r = 1
Output: 1
Explanation: As the source and destination both are same, there is only one possible way to stay there.Input: p = 2, q = 2, r = 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)
Table of Content
The recursive approach can be described with three potential moves from a given cell (i, j, k), where we can move to the next cells in three directions: (i+1, j, k), (i, j+1, k), or (i, j, k+1). The recurrence relation can be mathematically defined as:
Base Case: If the indices reach the last cell, i.e., when i = p-1, j = q-1 and k = r-1, the number of ways is 1: numberOfWays(i, j, k) = 1
6
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure:
The number of unique possible paths to reach the cell (i, j, k) depends on the optimal solutions of the smaller subproblems. Specifically, the number of ways to reach (i, j, k) is determined by the number of ways to reach the adjacent cells (i+1, j, k), (i, j+1, k), and (i, j, k+1). By combining these optimal substructures, we can efficiently calculate the total number of ways to reach the destination cell (p-1, q-1, r-1).
2. Overlapping Subproblems:
When applying a recursive approach to this problem, we observe that many subproblems are computed multiple times, leading to overlapping subproblems. For instance, when calculating the number of paths to a particular cell, say numberOfPaths(3, 3, 3), we may recursively calculate numberOfPaths(3, 2, 3) and numberOfPaths(2, 3, 3). Both of these calculations, in turn, would recursively compute numberOfPaths(2, 2, 3) multiple times—once from numberOfPaths(3, 2, 3) and again from numberOfPaths(2, 3, 3). This redundancy leads to overlapping subproblems.
6
The approach here is similar to the recursive solution but, instead of breaking down the problem recursively, we use an iterative approach to build up the solution from the base cases in a bottom-up manner.
To solve the 3D path problem iteratively, we maintain a dp[i][j][k] table where each entry dp[i][j][k] stores the count of all possible paths to reach the cell (i, j, k).
Base Case: For i == p-1 and j == q-1 and k == r-1 , dp[i][j][k] = 1
Recursive Case: dp[i][j][k] = dp[i-1][j][k] + dp[i][j-1][k] + dp[i][j][k-1];
6
In previous approach we can see that the dp[i][j][k] is depend upon dp[i - 1][j][k], dp[i][j - 1][k] and dp[i][j][k - 1] so we can convert 3d matrix into current and previous 2d matrix where i-1 refer to previous and i refer to current.
6