The problem is to print all the possible paths from top left to bottom right of an mXn matrix with the constraints that from each cell you can either move up, right, left or down.
Input : 1 2 3 4 5 6 Output : 1 2 3 6 1 2 5 6 1 4 5 6 4 5 2 3 6 Input : 1 2 3 4 5 6 7 8 9 Output : 1 2 3 6 9 1 2 3 6 5 8 9 1 2 3 6 5 4 7 8 9 1 2 5 6 9 1 2 5 8 9 1 2 5 4 7 8 9 1 4 5 6 9 1 4 5 8 9 1 4 5 2 3 6 9 1 4 7 8 9
This problem is mainly an extension of Count all paths from top left to bottom right in a matrix with two moves allowed
The algorithm is a simple recursive algorithm, from each cell first print all paths by going down and then print all paths by going right then print all paths by going up then print all paths by going left. Do this recursively for each cell encountered. There we will use Hash matrix for it will not repeat the same path which already traversed.
Following is C++ implementation of the above algorithm.
1 2 3 6 1 2 5 6 1 4 5 6 1 4 5 2 3 6
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print all possible paths from top left to bottom right of a mXn matrix
- Print all palindromic paths from top left to bottom right in a matrix
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Count all possible paths from top left to bottom right of a mXn matrix
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Maximum points from top left of matrix to bottom right and return back
- Lexicographically largest prime path from top-left to bottom-right in a matrix
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Print a matrix in alternate manner (left to right then right to left)
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Find the maximum cost path from the bottom-left corner to the top-right corner
- Maximum sum path in a matrix from top to bottom
- Maximum sum path in a matrix from top to bottom and back
- Print Stack Elements from Bottom to Top
- Print Bottom-Right View of a Binary Tree
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Count of palindromic plus paths in a given Matrix
- Number of palindromic paths in a matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.