## # 174. Dungeon Game

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was initially positioned in the top-left room and must fight his way through the dungeon to rescue the princess.

The knight has an initial health point represented by a positive integer. If at any point his health point drops to 0 or below, he dies immediately.

Some of the rooms are guarded by demons, so the knight loses health (negative integers) upon entering these rooms; other rooms are either empty (0's) or contain magic orbs that increase the knight's health (positive integers).

In order to reach the princess as quickly as possible, the knight decides to move only rightward or downward in each step.

Write a function to determine the knight's minimum initial health so that he is able to rescue the princess.

For example, given the dungeon below, the initial health of the knight must be at least 7 if he follows the optimal path RIGHT-> RIGHT -> DOWN -> DOWN.

-2(K) -3 3
-5 -10 1
10 30 -5(P)

### # Solution

Approach 1: DP -- working backwards from the bottom right corner.

### # Code (Python)

Approach 1:

``````    def calculateMinimumHP(self, dungeon: List[List[int]]) -> int:
if not dungeon or not dungeon:
return 1

# hp[i][j]: min health points needed when arriving at dungeon[i][j]
hp = [[0 for _ in range(len(dungeon))] for _ in range(len(dungeon))]

# need at least 1 health point when arriving at the final cell
hp[-1][-1] = max(1, 1 - dungeon[-1][-1])

for col in range(len(dungeon) - 2, -1, -1):
# need hp[-1][col+1] points at the beginning of arriving at the next cell
hp[-1][col] = max(1, hp[-1][col+1] - dungeon[-1][col])
for row in range(len(dungeon) - 2, -1, -1):
hp[row][-1] = max(1, hp[row+1][-1] - dungeon[row][-1])

for row in range(len(dungeon) - 2, -1, -1):
for col in range(len(dungeon) - 2, -1, -1):
hp[row][col] = max(1, min(hp[row+1][col], hp[row][col+1]) - dungeon[row][col])

return hp
``````

### # Code (C++)

Approach 1:

``````class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
int n = dungeon.size();
int hp[m][n];
for (int i = m-1; i >= 0; --i)
{
for (int j = n-1; j >= 0; --j)
{
hp[i][j] = (dungeon[i][j] > 0) ? 1 : 1 - dungeon[i][j];
int nextMin = INT_MAX;
if (i < m-1)
nextMin = std::min(nextMin, hp[i+1][j]);
if (j < n-1)
nextMin = std::min(nextMin, hp[i][j+1]);
if (i < m-1 || j < n-1)
hp[i][j] = std::max(hp[i][j], nextMin - dungeon[i][j]);
}
}
return hp;
}
};

class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m = dungeon.size();
int n = dungeon.size();
int hp[n];
for (int i = m-1; i >= 0; --i)
{
for (int j = n-1; j >= 0; --j)
{
int nextMin = INT_MAX;
if (i < m-1)
nextMin = std::min(nextMin, hp[j]);
if (j < n-1)
nextMin = std::min(nextMin, hp[j+1]);
hp[j] = (dungeon[i][j] > 0) ? 1 : 1 - dungeon[i][j];
if (i < m-1 || j < n-1)
hp[j] = std::max(hp[j], nextMin - dungeon[i][j]);
}
}
return hp;
}
};
``````