Print all possible paths from top left to bottom right of a mXn matrix | C ++ program to Print all possible paths from top left to bottom right of a mXn matrix ; If we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; If we hit this block , it means one path is completed . Add it to paths list and print ; If we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) If we hit this block , it means one path is completed . Add it to paths list and print ; Add current element to the path list path . append ( maze [ i ] [ j ] ) ; Move down in y direction and call findPathsUtil recursively ; Move down in y direction and call findPathsUtil recursively ;  ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > allPaths ; void findPathsUtil ( vector < vector < int > > maze , int m , int n , int i , int j , vector < int > path , int indx ) { if ( i == m - 1 ) { for ( int k = j ; k < n ; k ++ ) { path [ indx + k - j ] = maze [ i ] [ k ] ; } cout << " [ " << path [ 0 ] << " , ▁ " ; for ( int z = 1 ; z < path . size ( ) - 1 ; z ++ ) { cout << path [ z ] << " , ▁ " ; } cout << path [ path . size ( ) - 1 ] << " ] " << endl ; allPaths . push_back ( path ) ; return ; } if ( j == n - 1 ) { for ( int k = i ; k < m ; k ++ ) { path [ indx + k - i ] = maze [ k ] [ j ] ; } cout << " [ " << path [ 0 ] << " , ▁ " ; for ( int z = 1 ; z < path . size ( ) - 1 ; z ++ ) { cout << path [ z ] << " , ▁ " ; } cout << path [ path . size ( ) - 1 ] << " ] " << endl ; allPaths . push_back ( path ) ; return ; } path [ indx ] = maze [ i ] [ j ] ; findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) ; findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) ; } void findPaths ( vector < vector < int > > maze , int m , int n ) { vector < int > path ( m + n - 1 , 0 ) ; findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) ; } int main ( ) { vector < vector < int > > maze { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPaths ( maze , 3 , 3 ) ; return 0 ; }