https://www.geeksforgeeks.org/minimum-cells-traversed-reach-corner-every-cell-represents-jumps/
https://www.geeksforgeeks.org/minimum-cells-required-reach-destination-jumps-equal-cell-values/
Suppose A is at position (0, 0) of a 2-D grid containing ‘m’ rows and ‘n’ columns. His aim is to reach the bottom right point of this grid traveling through as minimum number of cells as possible.
Each cell of the grid contains a positive integer that defines the number of cells A can jump either in the right or the downward direction when he reaches that cell.
Find the minimum no of cells that need to be touched in order to reach bottom right corner.
Examples:
Input : 2 4 2 5 3 8 1 1 1 Output : So following two paths exist to reach (2, 2) from (0, 0) (0, 0) => (0, 2) => (2, 2) (0, 0) => (2, 0) => (2, 1) => (2, 2) Hence the output for this test case should be 3
Following is a Breadth First Search(BFS) solution of the problem:
- Think of this matrix as tree and (0, 0) as root and apply BFS using level order traversal.
- Push the coordinates and no of jumps in a queue.
- Pop the queue after every level of tree.
- Add the value at cell to the coordinates while traversing right and downward direction.
- Return no of cells touched while jumping when it reaches bottom right corner.
Time Complexity : O(n)
https://www.geeksforgeeks.org/minimum-cells-required-reach-destination-jumps-equal-cell-values/
Time Complexity: O(m*n)
Auxiliary Space: O(m*n)
Auxiliary Space: O(m*n)