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.
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)
Auxiliary Space : O(n)
This article is contributed by Kshitiz Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum cells required to reach destination with jumps equal to cell values
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Find if a 2-D array is completely traversed or not by following the cell values
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Minimum number of flips required such that the last cell of matrix can be reached from any other cell
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Maximize path sum from top-left cell to all other cells of a given Matrix
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Minimum jumps to reach last building in a matrix
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Sum of cost of all paths to reach a given cell in a Matrix
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups