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