Minimum cells traversed to reach corner where every cell represents jumps
Last Updated :
21 Mar, 2023
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.
Approach:
- Create a function matrixJump which takes a 2D array M and two integers R1 and C1 as input.
- Create a queue q to store the number of cells and coordinates (x, y).
- Push the starting position (R1, C1) and the number of cells (1) to the queue.
- While the queue is not empty, do the following:
a. Dequeue the front element of the queue, and set x, y, and no_of_cells to the corresponding values.
b. If the current position (x, y) is the bottom right cell, return the number of cells no_of_cells.
c. Otherwise, get the value of the current position in the matrix M, and for each of the two possible moves (x+v, y) and (x, y+v), where v is the value of the current position, do the following:
i. If the move is valid (i.e., the new position is within the matrix bounds), push the new position and the updated number of cells (no_of_cells+1) to the queue.
- If the bottom right cell cannot be reached, return -1.
Pseudocode:
function matrixJump(M, R1, C1):
q = queue of pairs of integers and a pair of integers
q.push((1, (R1, C1)))
while not q.empty():
no_of_cells, (x, y) = q.front()
q.pop()
if x == R-1 and y == C-1:
return no_of_cells
v = M[x][y]
if safe(x + v, y):
q.push((no_of_cells+1, (x+v, y)))
if safe(x, y + v):
q.push((no_of_cells+1, (x, y+v)))
return -1
function safe(x, y):
return x < R and y < C and x >= 0 and y >= 0
Implementation:
C++
Java
Python3
C#
Javascript
<script>
let R = 3, C = 3;
function safe(x,y)
{
if (x < R && y < C && x >= 0 && y >= 0)
return true ;
return false ;
}
class pair
{
constructor(t,r)
{
this .first = t;
this .second = r;
}
}
function matrixJump(M,R1,C1)
{
let q=[];
q.push( new pair(1, new pair(R1, C1)));
while (q.length > 0)
{
let x = q[0].second.first;
let y = q[0].second.second;
let no_of_cells = q[0].first;
q.shift();
if (x == R - 1 && y == C - 1)
return no_of_cells;
let v = M[x][y];
if (safe(x + v, y))
q.push( new pair(no_of_cells + 1,
new pair(x + v, y)));
if (safe(x, y + v))
q.push( new pair(no_of_cells + 1,
new pair(x, y + v)));
}
return -1;
}
let M=[[ 2, 4, 2 ],[ 5, 3, 8 ],[ 1, 1, 1 ]];
document.write( matrixJump(M, 0, 0));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(n)
Share your thoughts in the comments
Please Login to comment...