Minimum jumps to reach last building in a matrix | Recursive PHP 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
< ? php $ R = 4 ; $ C = 3 ; function isSafe ( $ x , $ y ) { global $ R , $ C ; return ( $ x < $ R and $ y < $ C ) ; } function minJump ( $ height , $ x , $ y ) { global $ R , $ C ; if ( $ x == $ R - 1 and $ y == $ C - 1 ) return 0 ; $ 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 ] ) ; $ down = PHP_INT_MAX ; if ( isSafe ( $ x + 1 , $ y ) ) $ down = minJump ( $ height , $ x + 1 , $ y ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x + 1 ] [ $ y ] ) ; $ right = PHP_INT_MAX ; if ( isSafe ( $ x , $ y + 1 ) ) $ right = minJump ( $ height , $ x , $ y + 1 ) + abs ( $ height [ $ x ] [ $ y ] - $ height [ $ x ] [ $ y + 1 ] ) ; return min ( $ down , min ( $ right , $ diag ) ) ; } $ height = array ( array ( 5 , 4 , 2 ) , array ( 9 , 2 , 1 ) , array ( 2 , 5 , 9 ) , array ( 1 , 3 , 11 ) ) ; echo minJump ( $ height , 0 , 0 ) ; ? >