## 题目描述：

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)

Notes:

The knight's health has no upper bound.
Any room can contain threats or power-ups, even the first room the knight enters and the bottom-right room where the princess is imprisoned.

## 题目大意：

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

## 解题思路：

```乍一看，这个问题和"Maximum/Minimum Path Sum"问题很相似。然而，具有全局最大HP（生命值）收益的路径并不一定可以保证最小的初始HP，因为题目中具有限制条件：HP不能≤0。例如，考虑下面的两条路径：0 -> -300 -> 310 -> 0 和 0 -> -1 -> 2 -> 0。这两条路径的净HP收益分别是-300 + 310 = 10 与 -1 + 2 = 1。第一条路径的净收益更高，但是它所需的初始HP至少是301，才能抵消第二个房间的-300HP损失，而第二条路径只需要初始HP为2就可以了。

min_HP_on_exit = min(D[i+1][j], D[i][j+1])

D[i][j] = max(min_HP_on_exit - dungeon[i][j], 1)
dungeon[i][j]可以为任意值。

D[0][0]就是最终答案。 此外，像许多其他"table-filling"问题一样，二维数组D可以用一维滚动数组替代。```

## Python代码：

``````class Solution:
# @param dungeon, a list of lists of integers
# @return a integer
def calculateMinimumHP(self, dungeon):
w = len(dungeon[0])
h = len(dungeon)
hp = [[0] * w for x in range(h)]

hp[h - 1][w - 1] = max(0, -dungeon[h - 1][w - 1]) + 1

for x in range(h - 1, -1, -1):
for y in range(w - 1, -1, -1):
down = None
if x + 1 < h:
down = max(1, hp[x + 1][y] - dungeon[x][y])
right = None
if y + 1 < w:
right = max(1, hp[x][y + 1] - dungeon[x][y])
if down and right:
hp[x][y] = min(down, right)
elif down:
hp[x][y] = down
elif right:
hp[x][y] = right
return hp[0][0]
``````

## Java代码（参考Leetcode Discuss中的解答）

``````public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
int m = dungeon.length;
int n = dungeon[0].length;

int[][] health = new int[m][n];

health[m - 1][n - 1] = Math.max(1 - dungeon[m - 1][n - 1], 1);

for (int i = m - 2; i >= 0; i--) {
health[i][n - 1] = Math.max(health[i + 1][n - 1] - dungeon[i][n - 1], 1);
}

for (int j = n - 2; j >= 0; j--) {
health[m - 1][j] = Math.max(health[m - 1][j + 1] - dungeon[m - 1][j], 1);
}

for (int i = m - 2; i >= 0; i--) {
for (int j = n - 2; j >= 0; j--) {
int down = Math.max(health[i + 1][j] - dungeon[i][j], 1);
int right = Math.max(health[i][j + 1] - dungeon[i][j], 1);
health[i][j] = Math.min(right, down);
}
}
return health[0][0];
}
}``````

Pingbacks已关闭。