Given a “m x n” matrix, count number of paths to reach bottom right from top left with maximum k turns allowed.
What is a turn? A movement is considered turn, if we were moving along row and now move along column. OR we were moving along column and now move along row.
There are two possible scenarios when a turn can occur at point (i, j): Turns Right: (i-1, j) -> (i, j) -> (i, j+1) Down Right Turns Down: (i, j-1) -> (i, j) -> (i+1, j) Right Down
Input: m = 3, n = 3, k = 2 Output: 4 See below diagram for four paths with maximum 2 turns. Input: m = 3, n = 3, k = 1 Output: 2
We strongly recommend you to minimize your browser and try this yourself first.
This problem can be recursively computed using below recursive formula.
countPaths(i, j, k): Count of paths to reach (i,j) from (0, 0) countPathsDir(i, j, k, 0): Count of paths if we reach (i, j) along row. countPathsDir(i, j, k, 1): Count of paths if we reach (i, j) along column. The fourth parameter in countPathsDir() indicates direction. Value of countPaths() can be written as: countPaths(i, j, k) = countPathsDir(i, j, k, 0) + countPathsDir(i, j, k, 1) And value of countPathsDir() can be recursively defined as: // Base cases // If current direction is along row If (d == 0) // Count paths for two cases // 1) We reach here through previous row. // 2) We reach here through previous column, so number of // turns k reduce by 1. countPathsDir(i, j, k, d) = countPathsUtil(i, j-1, k, d) + countPathsUtil(i-1, j, k-1, !d); // If current direction is along column Else // Similar to above countPathsDir(i, j, k, d) = countPathsUtil(i-1, j, k, d) + countPathsUtil(i, j-1, k-1, !d);
We can solve this problem in Polynomial Time using Dynamic Programming. The idea is to use a 4 dimensional table dp[m][n][k][d] where m is number of rows, n is number of columns, k is number of allowed turns and d is direction.
Below is Dynamic Programming based implementation.
Number of paths is 4
Time complexity of above solution is O(m*n*k)
Thanks to Gaurav Ahirwar for suggesting this solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of subsequences of length atmost K containing distinct prime elements
- Count number of paths whose weight is exactly X and has at-least one edge of weight M
- Maximum sum of non-overlapping subarrays of length atmost K
- Length of the longest subsegment which is UpDown after inserting atmost one integer
- Longest subsequence having difference atmost K
- Count all possible paths from top left to bottom right of a mXn matrix
- Count of Fibonacci paths in a Binary tree
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
- Number of paths with exactly k coins
- Number of palindromic paths in a matrix
- Total number of decreasing paths in a matrix
- Number of different cyclic paths of length N in a tetrahedron
- Number of paths from source to destination in a directed acyclic graph
- Paths with maximum number of 'a' from (1, 1) to (X, Y) vertically or horizontally
- Number of Paths of Weight W in a K-ary tree
- Number of shortest paths to reach every cell from bottom-left cell in the grid