// The main function that prints all paths from top left to bottom right

// in a matrix 'mat' of size mXn

voidprintAllPaths(int*mat, intm, intn)

{

int*path = newint[m+n];

printAllPathsUtil(mat, 0, 0, m, n, path, 0);

}

// Driver program to test abve functions

intmain()

{

intmat[2][3] = { {1, 2, 3}, {4, 5, 6} };

printAllPaths(*mat, 2, 3);

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to Print all possible paths from

// top left to bottom right of a mXn matrix

publicclassMatrixTraversal

{

/* mat: Pointer to the starting of mXn matrix

i, j: Current position of the robot (For the first call use 0,0)

m, n: Dimentions of given the matrix

pi: Next index to be filed in path array

*path[0..pi-1]: The path traversed by robot till now (Array to hold the

path need to have space for at least m+n elements) */

privatestaticvoidprintMatrix(intmat[][], intm, intn,

inti, intj, intpath[], intidx)

{

path[idx] = mat[i][j];

// Reached the bottom of the matrix so we are left with

// only option to move right

if(i == m - 1)

{

for(intk = j + 1; k < n; k++)

{

path[idx + k - j] = mat[i][k];

}

for(intl = 0; l < idx + n - j; l++)

{

System.out.print(path[l] + " ");

}

System.out.println();

return;

}

// Reached the right corner of the matrix we are left with

// only the downward movement.

if(j == n - 1)

{

for(intk = i + 1; k < m; k++)

{

path[idx + k - i] = mat[k][j];

}

for(intl = 0; l < idx + m - i; l++)

{

System.out.print(path[l] + " ");

}

System.out.println();

return;

}

// Print all the paths that are possible after moving down

printMatrix(mat, m, n, i + 1, j, path, idx + 1);

// Print all the paths that are possible after moving right

printMatrix(mat, m, n, i, j + 1, path, idx + 1);

}

// Driver code

publicstaticvoidmain(String[] args)

{

intm = 2;

intn = 3;

intmat[][] = { { 1, 2, 3},

{ 4, 5, 6} };

intmaxLengthOfPath = m + n - 1;

printMatrix(mat, m, n, 0, 0, newint[maxLengthOfPath], 0);

}

}

chevron_right

filter_none

Python3

# Python3 program to Prall possible paths from
# top left to bottom right of a mXn matrix

”’
/* mat: Pointer to the starting of mXn matrix
i, j: Current position of the robot
(For the first call use 0, 0)
m, n: Dimentions of given the matrix
pi: Next index to be filed in path array
*path[0..pi-1]: The path traversed by robot till now
(Array to hold the path need to have
space for at least m+n elements) */
”’
def printAllPathsUtil(mat, i, j, m, n, path, pi):

# Reached the bottom of the matrix
# so we are left with only option to move right
if (i == m – 1):
for k in range(j, n):
path[pi + k – j] = mat[i][k]

for l in range(pi + n – j):
print(path[l], end = ” “)
print()
return

# Reached the right corner of the matrix
# we are left with only the downward movement.
if (j == n – 1):

for k in range(i, m):
path[pi + k – i] = mat[k][j]

for l in range(pi + m – i):
print(path[l], end = ” “)
print()
return

# Add the current cell
# to the path being generated
path[pi] = mat[i][j]

# Print all the paths
# that are possible after moving down
printAllPathsUtil(mat, i + 1, j, m, n, path, pi + 1)

*path[0..pi-1]: The path traversed by robot till now (Array to hold the

path need to have space for at least m+n elements) */

privatestaticvoidprintMatrix(int[,]mat, intm, intn,

inti, intj, int[]path, intidx)

{

path[idx] = mat[i,j];

// Reached the bottom of the matrix so we are left with

// only option to move right

if(i == m - 1)

{

for(intk = j + 1; k < n; k++)

{

path[idx + k - j] = mat[i,k];

}

for(intl = 0; l < idx + n - j; l++)

{

Console.Write(path[l] + " ");

}

Console.WriteLine();

return;

}

// Reached the right corner of the matrix we are left with

// only the downward movement.

if(j == n - 1)

{

for(intk = i + 1; k < m; k++)

{

path[idx + k - i] = mat[k,j];

}

for(intl = 0; l < idx + m - i; l++)

{

Console.Write(path[l] + " ");

}

Console.WriteLine();

return;

}

// Print all the paths that are possible after moving down

printMatrix(mat, m, n, i + 1, j, path, idx + 1);

// Print all the paths that are possible after moving right

printMatrix(mat, m, n, i, j + 1, path, idx + 1);

}

// Driver code

publicstaticvoidMain(String[] args)

{

intm = 2;

intn = 3;

int[,]mat = { { 1, 2, 3 },

{ 4, 5, 6 } };

intmaxLengthOfPath = m + n - 1;

printMatrix(mat, m, n, 0, 0, newint[maxLengthOfPath], 0);

}

}

// This code contributed by Rajput-Ji

chevron_right

filter_none

Output:

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

Note that in the above code, the last line of printAllPathsUtil() is commented, If we uncomment this line, we get all the paths from the top left to bottom right of a nXm matrix if the diagonal movements are also allowed. And also if moving to some of the cells are not permitted then the same code can be improved by passing the restriction array to the above function and that is left as an exercise.

This article is contributed by Hariprasad NG. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above