Minimum Initial Points to Reach Destination | PHP program to find minimum initial points to reach destination ; dp [ i ] [ j ] represents the minimum initial points player should have so that when starts with cell ( i , j ) successfully reaches the destination cell ( m - 1 , n - 1 ) ; Base case ; Fill last row and last column as base to fill entire table ; fill the table in bottom - up fashion ; Driver Code
< ? php $ R = 3 ; $ C = 3 ; function minInitialPoints ( $ points ) { global $ R ; global $ C ; $ dp [ $ R ] [ $ C ] = array ( ) ; $ m = $ R ; $ n = $ C ; $ dp [ $ m - 1 ] [ $ n - 1 ] = $ points [ $ m - 1 ] [ $ n - 1 ] > 0 ? 1 : abs ( $ points [ $ m - 1 ] [ $ n - 1 ] ) + 1 ; for ( $ i = $ m - 2 ; $ i >= 0 ; $ i -- ) $ dp [ $ i ] [ $ n - 1 ] = max ( $ dp [ $ i + 1 ] [ $ n - 1 ] - $ points [ $ i ] [ $ n - 1 ] , 1 ) ; for ( $ j = $ n - 2 ; $ j >= 0 ; $ j -- ) $ dp [ $ m - 1 ] [ $ j ] = max ( $ dp [ $ m - 1 ] [ $ j + 1 ] - $ points [ $ m - 1 ] [ $ j ] , 1 ) ; for ( $ i = $ m - 2 ; $ i >= 0 ; $ i -- ) { for ( $ j = $ n - 2 ; $ j >= 0 ; $ j -- ) { $ min_points_on_exit = min ( $ dp [ $ i + 1 ] [ $ j ] , $ dp [ $ i ] [ $ j + 1 ] ) ; $ dp [ $ i ] [ $ j ] = max ( $ min_points_on_exit - $ points [ $ i ] [ $ j ] , 1 ) ; } } return $ dp [ 0 ] [ 0 ] ; } $ points = array ( array ( -2 , -3 , 3 ) , array ( -5 , -10 , 1 ) , array ( 10 , 30 , -5 ) ) ; echo " Minimum ▁ Initial ▁ Points ▁ Required : ▁ " , minInitialPoints ( $ points ) ; ? >