-
Notifications
You must be signed in to change notification settings - Fork 12
/
Print all possible paths from top left to bottom right of a mXn matrix
66 lines (57 loc) · 1.85 KB
/
Print all possible paths from top left to bottom right of a mXn matrix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// C++ program to Print all possible paths from
// top left to bottom right of a mXn matrix
#include<iostream>
using namespace std;
/* mat: Pointer to the starting of mXn matrix
i, j: Current position of the robot (For the first call use 0,0)
m, n: Dimensions 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) */
void printAllPathsUtil(int *mat, int i, int j, int m, int n, int *path, int pi)
{
// Reached the bottom of the matrix so we are left with
// only option to move right
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;
}
// Reached the right corner of the matrix we are left with
// only the downward movement.
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;
}
// Add the current cell to the path being generated
path[pi] = *((mat + i*n) + j);
// Print all the paths that are possible after moving down
printAllPathsUtil(mat, i+1, j, m, n, path, pi + 1);
// Print all the paths that are possible after moving right
printAllPathsUtil(mat, i, j+1, m, n, path, pi + 1);
// Print all the paths that are possible after moving diagonal
// printAllPathsUtil(mat, i+1, j+1, m, n, path, pi + 1);
}
// The main function that prints all paths from
// top left to bottom right in a matrix 'mat' of size mXn
void printAllPaths(int *mat, int m, int n)
{
int *path = new int[m+n];
printAllPathsUtil(mat, 0, 0, m, n, path, 0);
}
// Driver program to test above functions
int main()
{
int mat[2][3] = { {1, 2, 3}, {4, 5, 6} };
printAllPaths(*mat, 2, 3);
return 0;
}