# Minimum cells traversed to reach corner where every cell represents jumps

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.

## C++

`// C++ program to reach bottom right corner using ` `// minimum jumps. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define R 3 ` `#define C 3 ` ` ` `// function to check coordinates are in valid range. ` `bool` `safe(` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(x < R && y < C && x >= 0 && y >= 0) ` ` ` `return` `true` `; ` ` ` `return` `false` `; ` `} ` ` ` `// function to return minimum no of cells to reach ` `// bottom right cell. ` `int` `matrixJump(` `int` `M[R][C], ` `int` `R1, ` `int` `C1) ` `{ ` ` ` `queue<pair<` `int` `, pair<` `int` `, ` `int` `> > > q; ` ` ` ` ` `// push the no of cells and coordinates in a queue. ` ` ` `q.push(make_pair(1, make_pair(R1, C1))); ` ` ` ` ` `while` `(!q.empty()) { ` ` ` `int` `x = q.front().second.first; ` `// x coordinate ` ` ` `int` `y = q.front().second.second; ` `// y coordinate ` ` ` `int` `no_of_cells = q.front().first; ` `// no of cells ` ` ` ` ` `q.pop(); ` ` ` ` ` `// when it reaches bottom right return no of cells ` ` ` `if` `(x == R - 1 && y == C - 1) ` ` ` `return` `no_of_cells; ` ` ` ` ` `int` `v = M[x][y]; ` ` ` ` ` `if` `(safe(x + v, y)) ` ` ` `q.push(make_pair(no_of_cells + 1, make_pair(x + v, y))); ` ` ` ` ` `if` `(safe(x, y + v)) ` ` ` `q.push(make_pair(no_of_cells + 1, make_pair(x, y + v))); ` ` ` `} ` ` ` ` ` `// when destination cannot be reached ` ` ` `return` `-1; ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `int` `M[R][C] = { { 2, 4, 2 }, ` ` ` `{ 5, 3, 8 }, ` ` ` `{ 1, 1, 1 } }; ` ` ` `cout << matrixJump(M, 0, 0); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to reach bottom ` `# right corner using minimum jumps. ` ` ` `R, C ` `=` `3` `, ` `3` ` ` `# Function to check coordinates are in valid range. ` `def` `safe(x, y): ` ` ` ` ` `if` `x < R ` `and` `y < C ` `and` `x >` `=` `0` `and` `y >` `=` `0` `: ` ` ` `return` `True` ` ` `return` `False` ` ` `# Function to return minimum no of ` `# cells to reach bottom right cell. ` `def` `matrixJump(M, R1, C1): ` ` ` ` ` `q ` `=` `[] ` ` ` ` ` `# push the no of cells and coordinates in a queue. ` ` ` `q.append((` `1` `, (R1, C1))) ` ` ` ` ` `while` `len` `(q) !` `=` `0` `: ` ` ` `x ` `=` `q[` `0` `][` `1` `][` `0` `] ` `# x coordinate ` ` ` `y ` `=` `q[` `0` `][` `1` `][` `1` `] ` `# y coordinate ` ` ` `no_of_cells ` `=` `q[` `0` `][` `0` `] ` `# no of cells ` ` ` ` ` `q.pop(` `0` `) ` ` ` ` ` `# when it reaches bottom right return no of cells ` ` ` `if` `x ` `=` `=` `R ` `-` `1` `and` `y ` `=` `=` `C ` `-` `1` `: ` ` ` `return` `no_of_cells ` ` ` ` ` `v ` `=` `M[x][y] ` ` ` ` ` `if` `safe(x ` `+` `v, y): ` ` ` `q.append((no_of_cells ` `+` `1` `, (x ` `+` `v, y))) ` ` ` ` ` `if` `safe(x, y ` `+` `v): ` ` ` `q.append((no_of_cells ` `+` `1` `, (x, y ` `+` `v))) ` ` ` ` ` `# when destination cannot be reached ` ` ` `return` `-` `1` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `M ` `=` `[[` `2` `, ` `4` `, ` `2` `], ` ` ` `[` `5` `, ` `3` `, ` `8` `], ` ` ` `[` `1` `, ` `1` `, ` `1` `]] ` ` ` ` ` `print` `(matrixJump(M, ` `0` `, ` `0` `)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

**Output:**

3

**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 contribute@geeksforgeeks.org. 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.

## Recommended Posts:

- Minimum cells required to reach destination with jumps equal to cell values
- Find if a 2-D array is completely traversed or not by following the cell values
- Minimum jumps to reach last building in a matrix
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Minimum distance to the corner of a grid from source
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Find the minimum number of moves needed to move from one cell of matrix to another
- Minimum step to reach one
- Minimum steps to reach a destination
- Minimum steps to reach end of array under constraints
- Minimum steps to reach target by a Knight | Set 1
- Minimum Initial Points to Reach Destination