Given a MxN grid, how many paths can there be to reach the bottom right cell from the top left cell?
The only constraints are one cannot visit a cell more than once, I tried checking the other solutions but they only consider right and down moves whereas this one can have left and up moves too. Any help is appreciated 🙂
For example, consider a 3×3 matrix,
I converted into a graph as shown below:
where the numbers associated with node represent cell number counting from top left to bottom right, then the number of paths would be 12
as shown below:
{
{1, 2, 3, 6, 5, 4, 7, 8, 9}, {1, 2, 3, 6, 5, 8, 9}, {1, 2, 3, 6, 9},
{1, 2, 5, 4, 7, 8, 9}, {1, 2, 5, 6, 9}, {1, 2, 5, 8, 9},
{1, 4, 5, 2, 3, 6, 9}, {1, 4, 5, 6, 9}, {1, 4, 5, 8, 9},
{1, 4, 7, 8, 5, 2, 3,6, 9}, {1, 4, 7, 8, 5, 6, 9}, {1, 4, 7, 8, 9}
}
The above paths are shown below:
Even though I show each and every path here, I don't need them , I just need the count stating number of paths possible
Best Answer
If m==n you can find the no of paths to reach the bottom right cell from the top left cell using slight modification of Backtracking problem. Find below the code in Java.