Print all paths from top left to bottom right in a matrix with four moves allowed

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.

Examples:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Print All path from top left to bottom right
#include <iostream>
#include <vector>
using namespace std;
  
// Function to print all path
void printAllPath(vector<vector<int> > vec, 
                  vector<vector<int> > hash,
          int i, int j, vector<int> res = {})
{
    // check Condition
    if (i < 0 || j < 0 || i >= vec.size() || 
       j >= vec[0].size() || hash[i][j] == 1)
        return;
  
    // once it get the position (bottom right) 
    // than print the path
    if (i == vec.size() - 1 && j == vec[0].size() - 1) {
  
        // push the last element
        res.push_back(vec[i][j]);
        int k;
  
        // print the path
        for (k = 0; k < res.size(); k++)
            cout << res[k] << " ";
  
        cout << "\n";
  
        return;
    }
  
    // if the path is traverse already then
    // it will not go again the same path
    hash[i][j] = 1;
  
    // store the path
    res.push_back(vec[i][j]);
  
    // go to the right
    printAllPath(vec, hash, i, j + 1, res);
  
    // go to the down
    printAllPath(vec, hash, i + 1, j, res);
  
    // go to the up
    printAllPath(vec, hash, i - 1, j, res);
  
    // go to the left
    printAllPath(vec, hash, i, j - 1, res);
  
    // pop the last element
    res.pop_back();
  
    // hash position 0 for traverse another path
    hash[i][j] = 0;
}
  
// Driver code
int main()
{
    // Given matrix
    vector<vector<int> > vec = { { 1, 2, 3 },
                                 { 4, 5, 6 } };
  
    // mxn(2x3) 2d hash matrix
    vector<vector<int> > hash(2, vector<int>(3, 0));
  
    // print All Path of matrix
    printAllPath(vec, hash, 0, 0);
  
    return 0;
}

chevron_right


Output:

1 2 3 6 
1 2 5 6 
1 4 5 6 
1 4 5 2 3 6


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.