I know that there are algorithms to find the shortest path between two points, for example, algorithms answered in How to calculate the shortest path between two points in a grid.
However, now, I have a N * M
grid, where rows are from 0 to N - 1 and columns are from 0 to M - 1, where every grid contains obstacle (or you can think it as the distance between two grids). For example, I have a 4 * 4 grid below:
5 7 8 2
2 7 4 3
6 4 3 2
5 7 2 5
And I want to find the shortest distance between the left-top corner and the bottom row, i.e. between (0, 0)
and (X, 3)
, where X
can be any number from 0 to 3.
I can find out each shortest path between (0, 0) -> (0, 3)
to (0, 0) -> (3, 3)
, but this may be too slow. Is there a more efficient algorithm / approach to this question?