Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Minimum jumps to reach last building in a matrix

  • Difficulty Level : Medium
  • Last Updated : 06 Sep, 2021

Given a matrix containing an integer value, In which each cell of the matrix represents height of building. Find minimum jumps needed reach from First building (0, 0) to last (n-1, m-1). Jump from a cell to next cell is absolute difference between two building heights. 
 

Examples : 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input :  int height[][] = {{ 5, 4, 2 },
                           { 9, 2, 1 },
                           { 2, 5, 9 },
                           { 1, 3, 11}}; 
Output : 12
The minimum jump path is 5 -> 2 -> 5
-> 11. Total jumps is 3 + 3 + 6 = 12.

 



Naïve Recursive Solution: 
The above problem can be solved easily by using recursion. The path to reach (m, n) must be through one of the 3 cells: (m-1, n-1) or (m-1, n) or (m, n-1). So minimum jump to reach (m, n) can be written as “minimum jump of the 3 cells plus current jump.
Below is the implementation of the approach.

C++




// Recursive CPP program to find minimum jumps
// to reach last building from first.
#include <bits/stdc++.h>
using namespace std;
 
# define R 4
# define C 3
 
bool isSafe(int x, int y)
{
    return (x < R && y < C);
}
 
/* Returns minimum jump path from (0, 0) to
  (m, n) in height[R][C]*/
int minJump(int height[R][C], int x, int y)
{
    // base case
    if (x == R - 1 && y == C - 1)
        return 0;
 
    // Find minimum jumps if we go through diagonal
    int diag = INT_MAX;
    if (isSafe(x + 1, y + 1))
        diag = minJump(height, x + 1, y + 1) +
           abs(height[x][y] - height[x + 1][y + 1]);
 
    // Find minimum jumps if we go through down
    int down = INT_MAX;
    if (isSafe(x + 1, y))
        down = minJump(height, x + 1, y) +
             abs(height[x][y] - height[x + 1][y]);
 
    // Find minimum jumps if we go through right
    int right = INT_MAX;
    if (isSafe(x, y + 1))
        right = minJump(height, x, y + 1) +
              abs(height[x][y] - height[x][y + 1]);
 
    // return minimum jumps
    return min({down, right, diag});
}
 
/* Driver program to test above functions */
int main()
{
    int height[][C] = { { 5, 4, 2 },
                       { 9, 2, 1 },
                       { 2, 5, 9 },
                       { 1, 3, 11 } };
 
    cout << minJump(height, 0, 0);
    return 0;
}

Java




// Recursive Java program
// to find minimum jumps
// to reach last building
// from first.
class GFG {
     
    static boolean isSafe(int x, int y)
    {
        return (x < 4 && y < 3);
    }
     
    // Returns minimum jump
    // path from (0, 0) to
    // (m, n) in height[R][C]
    static int minJump(int height[][], int x,
                                       int y)
    {
        // base case
        if (x == 4 - 1 && y == 3 - 1)
            return 0;
     
        // Find minimum jumps
        // if we go through
        // diagonal
        int diag = Integer.MAX_VALUE;
         
        if (isSafe(x + 1, y + 1))
            diag = minJump(height, x + 1, y + 1) +
                   Math.abs(height[x][y] - height[x + 1][y + 1]);
     
        // Find minimum jumps
        // if we go through
        // down
        int down = Integer.MAX_VALUE;
         
        if (isSafe(x + 1, y))
            down = minJump(height, x + 1, y) +
                   Math.abs(height[x][y] - height[x + 1][y]);
     
        // Find minimum jumps
        // if we go through right
        int right = Integer.MAX_VALUE;
         
        if (isSafe(x, y + 1))
            right = minJump(height, x, y + 1) +
                    Math.abs(height[x][y] - height[x][y + 1]);
     
        // return minimum jumps
        return Math.min(down, Math.min(right, diag));
    }
     
    // Driver program
    public static void main(String[] args)
    {
        int height[][] = { { 5, 4, 2 },
                           { 9, 2, 1 },
                           { 2, 5, 9 },
                           { 1, 3, 11} };
     
        System.out.println(minJump(height, 0, 0));
    }
}
 
// This article is contributed by Prerna Saini.

Python3




# Recursive Python3 program to find minimum jumps
# to reach last building from first.
R = 4
C = 3
 
def isSafe(x, y):
    return (x < R and y < C)
 
# Returns minimum jump path from
# (0, 0) to (m, n) in height[R][C]
def minJump(height, x, y):
 
    # base case
    if (x == R - 1 and y == C - 1):
        return 0
 
    # Find minimum jumps if we go
    # through diagonal
    diag = 10**9
    if (isSafe(x + 1, y + 1)):
        diag = (minJump(height, x + 1, y + 1) +
                    abs(height[x][y] -
                        height[x + 1][y + 1]))
 
    # Find minimum jumps if we go through down
    down = 10**9
    if (isSafe(x + 1, y)):
        down = (minJump(height, x + 1, y) +
                    abs(height[x][y] -
                        height[x + 1][y]))
 
    # Find minimum jumps if we go through right
    right = 10**9
    if (isSafe(x, y + 1)):
        right = (minJump(height, x, y + 1) +
                     abs(height[x][y] -
                         height[x][y + 1]))
 
    # return minimum jumps
    return min([down, right, diag])
 
# Driver Code
height = [ [ 5, 4, 2 ],
           [ 9, 2, 1 ],
           [ 2, 5, 9 ],
           [ 1, 3, 11 ] ]
 
print(minJump(height, 0, 0))
 
# This code is contributed by mohit kumar

C#




// Recursive C# program
// to find minimum jumps
// to reach last building
// from first.
using System;
 
class GFG {
     
    static bool isSafe(int x, int y)
    {
        return (x < 4 && y < 3);
    }
     
    // Returns minimum jump
    // path from (0, 0) to
    // (m, n) in height[R][C]
    static int minJump(int [,]height,
                       int x, int y)
    {
         
        // base case
        if (x == 4 - 1 && y == 3 - 1)
            return 0;
     
        // Find minimum jumps
        // if we go through
        // diagonal
        int diag = int.MaxValue;
         
        if (isSafe(x + 1, y + 1))
            diag = minJump(height, x + 1, y + 1) +
                   Math.Abs(height[x,y] -
                   height[x + 1,y + 1]);
     
        // Find minimum jumps
        // if we go through
        // down
        int down = int.MaxValue;
         
        if (isSafe(x + 1, y))
            down = minJump(height, x + 1, y) +
                   Math.Abs(height[x,y] -
                   height[x + 1,y]);
     
        // Find minimum jumps
        // if we go through right
        int right = int.MaxValue;
         
        if (isSafe(x, y + 1))
            right = minJump(height, x, y + 1) +
                    Math.Abs(height[x,y] -
                    height[x,y + 1]);
     
        // return minimum jumps
        return Math.Min(down, Math.Min(right, diag));
    }
     
    // Driver code
    public static void Main()
    {
        int [,]height = {{5, 4, 2},
                        {9, 2, 1},
                        {2, 5, 9},
                        {1, 3, 11}};
     
        Console.Write(minJump(height, 0, 0));
    }
}
 
// This code is contributed by nitin mittal

PHP




<?php
 
// Recursive PHP program to
// find minimum jumps to
// reach last building from first.
$R = 4;
$C = 3;
 
function isSafe($x, $y)
{
    global $R, $C;
    return ($x < $R and $y < $C);
}
 
// Returns minimum jump
// path from (0, 0) to
// (m, n) in height[R][C]
function minJump($height, $x, $y)
{
    global $R, $C;
     
    // base case
    if ($x == $R - 1 and $y == $C - 1)
        return 0;
 
    // Find minimum jumps if
    // we go through diagonal
    $diag = PHP_INT_MAX;
    if (isSafe($x + 1, $y + 1))
        $diag = minJump($height, $x + 1, $y + 1) +
                             abs($height[$x][$y] -
                         $height[$x + 1][$y + 1]);
 
    // Find minimum jumps
    // if we go through down
    $down = PHP_INT_MAX;
    if (isSafe($x + 1, $y))
        $down = minJump($height, $x + 1, $y) +
                         abs($height[$x][$y] -
                         $height[$x + 1][$y]);
 
    // Find minimum jumps if
    // we go through right
    $right = PHP_INT_MAX;
    if (isSafe($x, $y + 1))
        $right = minJump($height, $x, $y + 1) +
                          abs($height[$x][$y] -
                          $height[$x][$y + 1]);
 
    // return minimum jumps
    return min($down, min($right, $diag));
}
 
// Driver Code
$height = array(array( 5, 4, 2 ),
                array( 9, 2, 1 ),
                array( 2, 5, 9 ),
                array( 1, 3, 11 ));
 
echo minJump($height, 0, 0);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// Recursive Javascript program to find minimum jumps
// to reach last building from first.
R = 4
C = 3
 
function isSafe(x, y)
{
    return (x < R && y < C);
}
 
/* Returns minimum jump path from (0, 0) to
  (m, n) in height[R][C]*/
function minJump(height, x, y)
{
    // base case
    if (x == R - 1 && y == C - 1)
        return 0;
 
    // Find minimum jumps if we go through diagonal
    var diag = 1000000000;
    if (isSafe(x + 1, y + 1))
        diag = minJump(height, x + 1, y + 1) +
           Math.abs(height[x][y] - height[x + 1][y + 1]);
 
    // Find minimum jumps if we go through down
    var down = 1000000000;
    if (isSafe(x + 1, y))
        down = minJump(height, x + 1, y) +
             Math.abs(height[x][y] - height[x + 1][y]);
 
    // Find minimum jumps if we go through right
    var right = 1000000000;
    if (isSafe(x, y + 1))
        right = minJump(height, x, y + 1) +
              Math.abs(height[x][y] - height[x][y + 1]);
 
    // return minimum jumps
    return Math.min(down, Math.min(right, diag));
}
 
/* Driver program to test above functions */
var height = [ [ 5, 4, 2 ],
                   [ 9, 2, 1 ],
                   [ 2, 5, 9 ],
                   [ 1, 3, 11 ] ];
document.write( minJump(height, 0, 0));
 
// This code is contributed by rutvik_56.
</script>

Output :  

12

Time complexity of this solution is exponential.
Dynamic Programming Solution: 
If we draw recursion tree of above recursive solution, we can observe overlapping subproblems. Since the problem has overlapping subproblems, we can solve it efficiently using Dynamic Programming. Below is Dynamic Programming based solution. 
 

C++




// Recursive CPP program to find minimum jumps
// to reach last building from first.
#include <bits/stdc++.h>
using namespace std;
 
# define R 4
# define C 3
 
bool isSafe(int x, int y)
{
    return (x < R && y < C);
}
 
/* Returns minimum jump path from (0, 0) to
  (m, n) in height[R][C]*/
int minJump(int height[R][C], int x, int y)
{
    // base case
    if (x == R - 1 && y == C - 1)
        return 0;
 
    // Find minimum jumps if we go through diagonal
    int diag = INT_MAX;
    if (isSafe(x + 1, y + 1))
        diag = minJump(height, x + 1, y + 1) +
           abs(height[x][y] - height[x + 1][y + 1]);
 
    // Find minimum jumps if we go through down
    int down = INT_MAX;
    if (isSafe(x + 1, y))
        down = minJump(height, x + 1, y) +
             abs(height[x][y] - height[x + 1][y]);
 
    // Find minimum jumps if we go through right
    int right = INT_MAX;
    if (isSafe(x, y + 1))
        right = minJump(height, x, y + 1) +
              abs(height[x][y] - height[x][y + 1]);
 
    // return minimum jumps
    return min({down, right, diag});
}
 
/* Driver program to test above functions */
int main()
{
    int height[][C] = { { 5, 4, 2 },
                       { 9, 2, 1 },
                       { 2, 5, 9 },
                       { 1, 3, 11 } };
 
    cout << minJump(height, 0, 0);
    return 0;
}

Java




// Recursive Java program
// to find minimum jumps
// to reach last building
// from first.
class GFG {
     
    static boolean isSafe(int x, int y)
    {
        return (x < 4 && y < 3);
    }
     
    // Returns minimum jump
    // path from (0, 0) to
    // (m, n) in height[R][C]
    static int minJump(int height[][], int x,
                                       int y)
    {
        // base case
        if (x == 4 - 1 && y == 3 - 1)
            return 0;
     
        // Find minimum jumps
        // if we go through
        // diagonal
        int diag = Integer.MAX_VALUE;
         
        if (isSafe(x + 1, y + 1))
            diag = minJump(height, x + 1, y + 1) +
                   Math.abs(height[x][y] - height[x + 1][y + 1]);
     
        // Find minimum jumps
        // if we go through
        // down
        int down = Integer.MAX_VALUE;
         
        if (isSafe(x + 1, y))
            down = minJump(height, x + 1, y) +
                   Math.abs(height[x][y] - height[x + 1][y]);
     
        // Find minimum jumps
        // if we go through right
        int right = Integer.MAX_VALUE;
         
        if (isSafe(x, y + 1))
            right = minJump(height, x, y + 1) +
                    Math.abs(height[x][y] - height[x][y + 1]);
     
        // return minimum jumps
        return Math.min(down, Math.min(right, diag));
    }
     
    // Driver program
    public static void main(String[] args)
    {
        int height[][] = { { 5, 4, 2 },
                           { 9, 2, 1 },
                           { 2, 5, 9 },
                           { 1, 3, 11} };
     
        System.out.println(minJump(height, 0, 0));
    }
}
 
// This article is contributed by Prerna Saini.

Python3




# Recursive Python3 program to find minimum jumps
# to reach last building from first.
R = 4
C = 3
 
def isSafe(x, y):
    return (x < R and y < C)
 
# Returns minimum jump path from
# (0, 0) to (m, n) in height[R][C]
def minJump(height, x, y):
 
    # base case
    if (x == R - 1 and y == C - 1):
        return 0
 
    # Find minimum jumps if we go
    # through diagonal
    diag = 10**9
    if (isSafe(x + 1, y + 1)):
        diag = (minJump(height, x + 1, y + 1) +
                    abs(height[x][y] -
                        height[x + 1][y + 1]))
 
    # Find minimum jumps if we go through down
    down = 10**9
    if (isSafe(x + 1, y)):
        down = (minJump(height, x + 1, y) +
                    abs(height[x][y] -
                        height[x + 1][y]))
 
    # Find minimum jumps if we go through right
    right = 10**9
    if (isSafe(x, y + 1)):
        right = (minJump(height, x, y + 1) +
                     abs(height[x][y] -
                         height[x][y + 1]))
 
    # return minimum jumps
    return min([down, right, diag])
 
# Driver Code
height = [ [ 5, 4, 2 ],
           [ 9, 2, 1 ],
           [ 2, 5, 9 ],
           [ 1, 3, 11 ] ]
 
print(minJump(height, 0, 0))
 
# This code is contributed by mohit kumar

C#




// Recursive C# program
// to find minimum jumps
// to reach last building
// from first.
using System;
 
class GFG {
     
    static bool isSafe(int x, int y)
    {
        return (x < 4 && y < 3);
    }
     
    // Returns minimum jump
    // path from (0, 0) to
    // (m, n) in height[R][C]
    static int minJump(int [,]height,
                       int x, int y)
    {
         
        // base case
        if (x == 4 - 1 && y == 3 - 1)
            return 0;
     
        // Find minimum jumps
        // if we go through
        // diagonal
        int diag = int.MaxValue;
         
        if (isSafe(x + 1, y + 1))
            diag = minJump(height, x + 1, y + 1) +
                   Math.Abs(height[x,y] -
                   height[x + 1,y + 1]);
     
        // Find minimum jumps
        // if we go through
        // down
        int down = int.MaxValue;
         
        if (isSafe(x + 1, y))
            down = minJump(height, x + 1, y) +
                   Math.Abs(height[x,y] -
                   height[x + 1,y]);
     
        // Find minimum jumps
        // if we go through right
        int right = int.MaxValue;
         
        if (isSafe(x, y + 1))
            right = minJump(height, x, y + 1) +
                    Math.Abs(height[x,y] -
                    height[x,y + 1]);
     
        // return minimum jumps
        return Math.Min(down, Math.Min(right, diag));
    }
     
    // Driver code
    public static void Main()
    {
        int [,]height = {{5, 4, 2},
                        {9, 2, 1},
                        {2, 5, 9},
                        {1, 3, 11}};
     
        Console.Write(minJump(height, 0, 0));
    }
}
 
// This code is contributed by nitin mittal

PHP




<?php
 
// Recursive PHP program to
// find minimum jumps to
// reach last building from first.
$R = 4;
$C = 3;
 
function isSafe($x, $y)
{
    global $R, $C;
    return ($x < $R and $y < $C);
}
 
// Returns minimum jump
// path from (0, 0) to
// (m, n) in height[R][C]
function minJump($height, $x, $y)
{
    global $R, $C;
     
    // base case
    if ($x == $R - 1 and $y == $C - 1)
        return 0;
 
    // Find minimum jumps if
    // we go through diagonal
    $diag = PHP_INT_MAX;
    if (isSafe($x + 1, $y + 1))
        $diag = minJump($height, $x + 1, $y + 1) +
                             abs($height[$x][$y] -
                         $height[$x + 1][$y + 1]);
 
    // Find minimum jumps
    // if we go through down
    $down = PHP_INT_MAX;
    if (isSafe($x + 1, $y))
        $down = minJump($height, $x + 1, $y) +
                         abs($height[$x][$y] -
                         $height[$x + 1][$y]);
 
    // Find minimum jumps if
    // we go through right
    $right = PHP_INT_MAX;
    if (isSafe($x, $y + 1))
        $right = minJump($height, $x, $y + 1) +
                          abs($height[$x][$y] -
                          $height[$x][$y + 1]);
 
    // return minimum jumps
    return min($down, min($right, $diag));
}
 
// Driver Code
$height = array(array( 5, 4, 2 ),
                array( 9, 2, 1 ),
                array( 2, 5, 9 ),
                array( 1, 3, 11 ));
 
echo minJump($height, 0, 0);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
// Recursive Javascript program to find minimum jumps
// to reach last building from first.
R = 4
C = 3
 
function isSafe(x, y)
{
    return (x < R && y < C);
}
 
/* Returns minimum jump path from (0, 0) to
  (m, n) in height[R][C]*/
function minJump(height, x, y)
{
    // base case
    if (x == R - 1 && y == C - 1)
        return 0;
 
    // Find minimum jumps if we go through diagonal
    var diag = 1000000000;
    if (isSafe(x + 1, y + 1))
        diag = minJump(height, x + 1, y + 1) +
           Math.abs(height[x][y] - height[x + 1][y + 1]);
 
    // Find minimum jumps if we go through down
    var down = 1000000000;
    if (isSafe(x + 1, y))
        down = minJump(height, x + 1, y) +
             Math.abs(height[x][y] - height[x + 1][y]);
 
    // Find minimum jumps if we go through right
    var right = 1000000000;
    if (isSafe(x, y + 1))
        right = minJump(height, x, y + 1) +
              Math.abs(height[x][y] - height[x][y + 1]);
 
    // return minimum jumps
    return Math.min(down, Math.min(right, diag));
}
 
/* Driver program to test above functions */
var height = [ [ 5, 4, 2 ],
                   [ 9, 2, 1 ],
                   [ 2, 5, 9 ],
                   [ 1, 3, 11 ] ];
document.write( minJump(height, 0, 0));
 
// This code is contributed by rutvik_56.
</script>

Output:  

12

Time complexity: (R*C)




My Personal Notes arrow_drop_up
Recommended Articles
Page :