

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print all possible paths from top left to bottom right of a mXn matrix in C++
In this problem, we are given an mXn 2D matrix and we have to print all possible paths from top left to the bottom right of the matrix. For traversal, we can move only right and down in the matrix.
Let’s take an example to understand the topic better −
Input: 1 3 5 2 8 9 Output: 1 -> 3 -> 5 -> 9 1 -> 3 -> 8 -> 9 1 -> 2 -> 8 -> 9
To solve this problem, we will move from one cell to another and print the path on going down and right. We will do this recursively for each cell in the matrix.
Example
Let’s see a program that implements the recursive algorithm :
#include<iostream> using namespace std; void printPathTPtoBR(int *mat, int i, int j, int m, int n, int *path, int pi) { if (i == m - 1){ for (int k = j; k < n; k++) path[pi + k - j] = *((mat + i*n) + k); for (int l = 0; l < pi + n - j; l++) cout << path[l] << " "; cout << endl; return; } if (j == n - 1){ for (int k = i; k < m; k++) path[pi + k - i] = *((mat + k*n) + j); for (int l = 0; l < pi + m - i; l++) cout << path[l] << " "; cout << endl; return; } path[pi] = *((mat + i*n) + j); printPathTPtoBR(mat, i+1, j, m, n, path, pi + 1); printPathTPtoBR(mat, i, j+1, m, n, path, pi + 1); } void findPath(int *mat, int m, int n) { int *path = new int[m+n]; printPathTPtoBR(mat, 0, 0, m, n, path, 0); } int main() { int mat[2][3] = { {1, 2, 3}, {4, 5, 6} }; cout<<"Path from top-left to bottom-rigth of matrix are :\n"; findPath(*mat, 2, 3); return 0; }
Output
The path from top-left to bottom-right of a matrix are −
1 4 5 6 1 2 5 6 1 2 3 6
- Related Questions & Answers
- Count all possible paths from top left to bottom right of a mXn matrix in C++
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- Maximum points from top left of matrix to bottom right and return back in C++
- Print a matrix in alternate manner (left to right then right to left) in C++
- Set the left, right, top and bottom padding of an element
- Print all leaf nodes of a binary tree from right to left in C++
- How to display the JList items from top to bottom and left to right in Java?
- Program to find number of ways we can reach from top left point to bottom right point in Python
- How to add a straight line to a plot in R starting from bottom left and ending at top right?
- Maximum sum path in a matrix from top to bottom in C++
- Print All Leaf Nodes of a Binary Tree from left to right using Iterative Approach in C++
- Program to count number of walls required to partition top-left and bottom-right cells in Python
- Print all paths from a given source to a destination in C++
- Maximum sum path in a matrix from top to bottom in C++ Program
Advertisements