# Minimum Initial Points to Reach Destination

## Introduction

One can frequently come across fascinating problems in the fields of computer science and algorithmic problem-solving that call for a deft application of logic and mathematics. One such issue is figuring out, on a grid, the minimal starting points required to arrive at a destination while guaranteeing that the player's points remain positive the entire way.

Envision moving across a grid with a specific number of points in each cell. These points may be zero, negative, or positive. Only when you have a positive point balance, that is, when your balance is greater than zero, can you move between cells. Your goal is to get to a particular destination cell while always having a positive point balance. Finding the minimal starting points needed to start this journey properly is the difficult part.

### Example

Our objective is to move from (0, 0) to the destination cell at (2, 2). To make sure that our point balance is positive the entire way, we must determine the minimal starting points needed.

### The Approach to Dynamic Programming

We can use a dynamic programming approach to solve this issue. The goal is to produce a 2D table, or dp, that is the same in size as the grid. The table shows that starting at cell (i, j), the minimal number of points needed to reach the destination is represented by dp[i][j].

Step 1: Base Case

Making a base case, in this case, for the target cell (m-1, n-1) is the first step. To get there securely, we need one point if the points in this cell are affirmative. To maintain a positive balance, we must add abs(points[m-1][n-1]) + 1 point to make up for the loss if the points are negative.

Step 2: Filling the Last Row and Last Column

After that, we may fill the dp table by going backward from the goal cell. We start by entering the values for the final row and column in order to do this.

In order to reach the cells on the right while keeping a positive balance, we compute the minimal number of points needed for the final row. Similarly, we calculate the minimal number of points required in the final column in order to reach the bottom cells without going negative.

Step 3: Filling the Rest of the Table

We can now fill the remaining cells of the dp table with the base cases and the values for the last row and column. Based on the lowest of the points required to depart the cells below (i, j+1) and to the right (i+1, j), we calculate the minimal points needed to exit each cell (i, j). This could be shown as:

min_points_on_exit = min(dp[i + 1][j], dp[i][j + 1])

Step 4: The dp[i][j] calculation

Let's now determine dp[i][j]'s value. To accomplish this, select the greatest of the two cases:

• A zero value for pts[i][j] indicates that there is no gain or loss in this cell, allowing the player to finish with the same number of points they started with (dp[i][j] = min_points_on_exit).
• The player must have more points than min_points_on_exit before entering the cell (i, j) if points[i][j] are negative in order to make up for the lost points. Since -points[i][j] is the minimal compensation needed, the formula is dp[i][j] = min_points_on_exit - points[i][j].
• The player could enter the cell (i, j) with as low as min_points_on_exit - points[i][j] if points[i][j] are positive since they will be gaining points[i][j] in this cell. We set dp[i][j] = max(min_points_on_exit - points[i][j], 1) to make sure that min_points_on_exit - points[i][j] doesn't decrease to zero or lower, though.

Step 5: Final Result

The value in dp[0][0] denotes the minimal starting points needed to arrive at the destination while continuing to accrue positive points all the way.

In the previous example, the player's points must be positive throughout the course. Hence, the least number of initial points needed was determined to be 7.

Code

Output:

Code Explanation

Minimum Initial Points Function

• Calculates the minimum initial points needed to reach the bottom-right cell in a grid (represented by points[R][C]) while maintaining a positive sum at each step.
• Uses dynamic programming (dp array) to compute the minimum points required at each cell.

Dynamic Programming Approach

• Starts from the bottom-right cell and iterates backward, computing the minimum points required to reach each cell.
• Determines the minimum points needed to exit the current cell, considering the points on the cell and the minimum points required at the next reachable cells.

Main Function

• Initializes a 3x3 grid (points) with specific values.
• Calls minInitialPoints to find the minimum initial points needed and prints the result.

Output

• Prints the minimum initial points required: Minimum Initial Points Required: 7.