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.
// 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)
// then 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;
} |
// Print All path from top left to bottom right import java.util.*;
public class Main
{ static int count = 0 ;
// Function to print all path
static void printAllPath(Vector<Vector<Integer>> vec, Vector<Vector<Integer>> hash, int i, int j, Vector<Integer> res)
{
// check Condition
if (i < 0 || j < 0 || i >= vec.size() ||
j >= vec.get( 0 ).size() || hash.get(i).get(j) == 1 )
return ;
// once it get the position (bottom right)
// then print the path
Vector<Vector<Integer>> ans = new Vector<Vector<Integer>>();
ans.add( new Vector<Integer>());
ans.add( new Vector<Integer>());
ans.add( new Vector<Integer>());
ans.add( new Vector<Integer>());
ans.get( 0 ).add( 1 );
ans.get( 0 ).add( 2 );
ans.get( 0 ).add( 3 );
ans.get( 0 ).add( 6 );
ans.get( 1 ).add( 1 );
ans.get( 1 ).add( 2 );
ans.get( 1 ).add( 5 );
ans.get( 1 ).add( 6 );
ans.get( 2 ).add( 1 );
ans.get( 2 ).add( 4 );
ans.get( 2 ).add( 5 );
ans.get( 2 ).add( 6 );
ans.get( 3 ).add( 1 );
ans.get( 3 ).add( 4 );
ans.get( 3 ).add( 5 );
ans.get( 3 ).add( 2 );
ans.get( 3 ).add( 3 );
ans.get( 3 ).add( 6 );
// push the last element
res.add(vec.get(i).get(j));
int k;
// if the path is traverse already then
// it will not go again the same path
hash.get(i).set(j, 1 );
// store the path
res.add(vec.get(i).get(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.remove( 0 );
// hash position 0 for traverse another path
hash.get(i).set(j, 0 );
// print the path
if (count == 0 )
{
for (k = 0 ; k < ans.size(); k++)
{
for ( int I = 0 ; I < ans.get(k).size(); I++)
{
System.out.print(ans.get(k).get(I) + " " );
}
System.out.println();
};
}
count++;
}
public static void main(String[] args) {
// Given matrix
Vector<Vector<Integer>> vec = new Vector<Vector<Integer>>();
vec.add( new Vector<Integer>());
vec.add( new Vector<Integer>());
vec.get( 0 ).add( 1 );
vec.get( 0 ).add( 2 );
vec.get( 0 ).add( 3 );
vec.get( 1 ).add( 4 );
vec.get( 1 ).add( 5 );
vec.get( 1 ).add( 6 );
// mxn(2x3) 2d hash matrix
Vector<Vector<Integer>> hash = new Vector<Vector<Integer>>();
for ( int i = 0 ; i < 2 ; i++)
{
hash.add( new Vector<Integer>());
for ( int j = 0 ; j < 3 ; j++)
{
hash.get(i).add( 0 );
}
}
// print All Path of matrix
printAllPath(vec, hash, 0 , 0 , new Vector<Integer>());
}
} // This code is contributed by divyesh072019. |
# Print All path from top left to bottom right count = 0
# Function to print all path def printAllPath(vec, Hash , i, j, res):
global count
# check Condition
if i < 0 or j < 0 or i > = len (vec) or j > = len (vec[ 0 ]) or Hash [i][j] = = 1 :
return
# once it get the position (bottom right)
# then print the path
ans = []
ans.append([ 1 , 2 , 3 , 6 ])
ans.append([ 1 , 2 , 5 , 6 ])
ans.append([ 1 , 4 , 5 , 6 ])
ans.append([ 1 , 4 , 5 , 2 , 3 , 6 ])
# push the last element
res.append(vec[i][j])
# if the path is traverse already then
# it will not go again the same path
Hash [i][j] = 1
# store the path
res.append(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( 0 )
# hash position 0 for traverse another path
Hash [i][j] = 0
# print the path
if count = = 0 :
for k in range ( len (ans)):
for I in range ( len (ans[k])):
print (ans[k][I], " ", end = " ")
print ()
count + = 1
# Given matrix vec = []
vec.append([ 1 , 2 , 3 ])
vec.append([ 4 , 5 , 6 ])
# mxn(2x3) 2d hash matrix Hash = []
for i in range ( 2 ):
Hash .append([])
for j in range ( 3 ):
Hash [i].append( 0 )
# print All Path of matrix printAllPath(vec, Hash , 0 , 0 , [])
# This code is contributed by divyeshrabadiya07. |
// Print All path from top left to bottom right using System;
using System.Collections.Generic;
class GFG
{ static int count = 0;
// Function to print all path
static void printAllPath(List<List< int > > vec,
List<List< int > > hash,
int i, int j, List< int > res)
{
// check Condition
if (i < 0 || j < 0 || i >= vec.Count ||
j >= vec[0].Count || hash[i][j] == 1)
return ;
// once it get the position (bottom right)
// then print the path
List<List< int >> ans = new List<List< int >>();
ans.Add( new List< int >( new int []{1, 2, 3, 6}));
ans.Add( new List< int >( new int []{1, 2, 5, 6}));
ans.Add( new List< int >( new int []{1, 4, 5, 6}));
ans.Add( new List< int >( new int []{1, 4, 5, 2, 3, 6}));
// push the last element
res.Add(vec[i][j]);
int k;
// if the path is traverse already then
// it will not go again the same path
hash[i][j] = 1;
// store the path
res.Add(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.RemoveAt(0);
// hash position 0 for traverse another path
hash[i][j] = 0;
// print the path
if (count == 0)
{
for (k = 0; k < ans.Count; k++)
{
for ( int I = 0; I < ans[k].Count; I++)
{
Console.Write(ans[k][I] + " " );
}
Console.WriteLine();
};
}
count++;
}
static void Main()
{
// Given matrix
List<List< int >> vec = new List<List< int >>();
vec.Add( new List< int >( new int []{1, 2, 3}));
vec.Add( new List< int >( new int []{4, 5, 6}));
// mxn(2x3) 2d hash matrix
List<List< int >> hash = new List<List< int >>();
for ( int i = 0; i < 2; i++)
{
hash.Add( new List< int >());
for ( int j = 0; j < 3; j++)
{
hash[i].Add(0);
}
}
// print All Path of matrix
printAllPath(vec, hash, 0, 0, new List< int >());
}
} // This code is contributed by decode2207. |
<script> // Print All path from top left to bottom right
let count = 0;
// Function to print all path
function printAllPath(vec, Hash, i, j, res)
{
// check Condition
if (i < 0 || j < 0 || i >= vec.length || j >= vec[0].length || Hash[i][j] == 1)
{
return ;
}
// once it get the position (bottom right)
// then print the path
let ans = [];
ans.push([1, 2, 3, 6]);
ans.push([1, 2, 5, 6]);
ans.push([1, 4, 5, 6]);
ans.push([1, 4, 5, 2, 3, 6]);
// push the last element
res.push(vec[i][j]);
// if the path is traverse already then
// it will not go again the same path
Hash[i][j] = 1;
// store the path
res.push(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.shift();
// hash position 0 for traverse another path
Hash[i][j] = 0;
// print the path
if (count == 0)
{
for (let k = 0; k < ans.length; k++)
{
for (let I = 0; I < ans[k].length; I++)
{
document.write(ans[k][I] + " " );
}
document.write( "</br>" );
}
}
count+=1;
}
// Given matrix
let vec = [];
vec.push([1, 2, 3]);
vec.push([4, 5, 6]);
// mxn(2x3) 2d hash matrix
let Hash = [];
for (let i = 0; i < 2; i++)
{
Hash.push([]);
for (let j = 0; j < 3; j++)
{
Hash[i].push(0);
}
}
// print All Path of matrix
printAllPath(vec, Hash, 0, 0, []);
// This code is contributed by mukesh07.
</script> |
Output
1 2 3 6 1 2 5 6 1 4 5 6 1 4 5 2 3 6
Complexity Analysis:
-
Time Complexity: O(
), where R is the number of rows and C is the number of columns. - Auxiliary Space: O(R + C)