1. 程式人生 > >[LeetCode] Dungeon Game 地牢遊戲

[LeetCode] 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)

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.

Credits:
Special thanks to @stellari for adding this problem and creating all test cases.

這道王子救公主的題還是蠻新穎的,我最開始的想法是比較右邊和下邊的數字的大小,去大的那個,但是這個演算法對某些情況不成立,比如下面的情況:

1 (K) -3 3
0 -2 0
-3 -3 -3 (P)

如果按我的那種演算法走的路徑為 1 -> 0 -> -2 -> 0 -> -3, 這樣的話騎士的起始血量要為5,而正確的路徑應為 1 -> -3 -> 3 -> 0 -> -3, 這樣騎士的騎士血量只需為3。無奈只好上網看大神的解法,發現統一都是用動態規劃Dynamic Programming來做,建立一個二維陣列dp,其中dp[i][j]用來表示當前位置 (i, j) 出發的起始血量,最先處理的是公主所在的房間的起始生命值,然後慢慢向第一個房間擴散,不斷的得到各個位置的最優的生命值。逆向推正是本題的精髓所在啊,仔細想想也是,如果從起始位置開始遍歷,我們並不知道初始時應該初始化的血量,但是到達公主房間後,我們知道血量至少不能小於1,如果公主房間還需要掉血的話,那麼掉血後剩1才能保證起始位置的血量最小。那麼下面來推導狀態轉移方程,首先考慮每個位置的血量是由什麼決定的,騎士會掛主要是因為去了下一個房間時,掉血量大於本身的血值,而能去的房間只有右邊和下邊,所以當前位置的血量是由右邊和下邊房間的可生存血量決定的,進一步來說,應該是由較小的可生存血量決定的,因為較我們需要起始血量儘可能的少,所以用較小的可生存血量減去當前房間的數字,如果是非正數的話,說明當前當前的房間數字是整數,那麼當前房間的生存血量可以是1,所以我們的狀態轉移方程是dp[i][j] = max(1, min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j])。為了更好的處理邊界情況,我們的二維dp陣列比原陣列的行數列數均多1個,先都初始化為整型數最大值INT_MAX,由於我們知道到達公主房間後,騎士火拼完的血量至少為1,那麼此時公主房間的右邊和下邊房間裡的數字我們就都設定為1,這樣到達公主房間的生存血量就是1減去公主房間的數字和1相比較,取較大值,就沒有問題了,程式碼如下:

解法一:

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

我們可以對空間進行優化,使用一個一維的dp陣列,並且不停的覆蓋原有的值,參見程式碼如下:

解法二:

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

類似題目:

參考資料: