跳转至

174. Dungeon Game

Leetcode Binary Search Dynamic Programming

The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M \times 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.

dungeonGame

Note:

  • 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.

分析

这个问题难点在于问题的定义。看完题目以后,可以很明显确定用动态规划方法,但是找到状态方程却不简单。

最开始,最直接也是最常见的定义状态或者子问题的方式为

dp[i][j] = 从(0,0)到(i,j)的最小健康点损失

这种定义与LeetCode 64. Minimum Path Sum的定义类似。但是很可惜,这样根本不能解决问题,因为dp[i][j]并不依赖于前面的子问题dp[i-1][j]和dp[i][j-1]。所以只能另辟蹊径了。另一种常见的定义状态和子问题的方法为从问题的另一端定义:

initialHealth[i][j] = 从(i,j)出发,到达公主(终点)所需最低健康值

首先,我们知道在任意一点(i,j),健康值达到0或者0一下,武士会立刻死亡,也就是说

initialHealth[i][j] > 0 or initialHealth[i][j] >= 1

另外,由于经过(i, j),武士的健康值会发生增加或者减少,幅度为dungeon[i][j],因此

// 从(i, j) 到达 (i+1, j)
initialHealth[i][j] + dungeon[i][j] >= initialHealth[i+1][j]
initialHealth[i][j] = max(1, initialHealth[i+1][j] - dungeon[i][j])

好了,下一步,我们观察子问题之间的关系,试图发现状态方程。由于在(i,j)点的武士下一步即可以去(i, j+1),也可以去(i+1, j)。所以要从两者之间选择一个需要初始健康值最小的路径。

initialHealth[i][j] = max(1, 
    min(initialHealth[i + 1][j], initialHealth[i][j + 1]) - dungeon[i][j]);

代码:

public int calculateMinimumHP(int[][] dungeon) {
    if (dungeon == null || dungeon.length == 0 || dungeon[0].length == 0) return 0;
    int m = dungeon.length, n = dungeon[0].length;
    int[][] initialHealth = new int[m][n];
    initialHealth[m - 1][n - 1] = Math.max(1,  1 - dungeon[m - 1][n - 1]);
    for (int i = m - 2; i >= 0; i--)
        initialHealth[i][n - 1] = Math.max(1,  
            initialHealth[i + 1][n - 1] - dungeon[i][n - 1]);

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

    for (int i = m - 2; i >= 0; i--)
        for (int j = n - 2; j >= 0; j--)
            initialHealth[i][j] = Math.max(1, 
                Math.min(initialHealth[i+1][j], initialHealth[i][j+1]) - dungeon[i][j]);

    return initialHealth[0][0];
}