Tuesday, January 27, 2015

Dungeon Game

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)-33
-5-101
1030-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.
--------------------------------------- thinking -------------------------------------------------------------------------
At the very beginning, I was thinking of this problem as DP problem, which is right. But the working direction is wrong. I was thinking of doing it on the increasing order. However, this problem should be resolved by back-forward order, from [m-1][n-1] to [0][0]. The reason is that we need to keep the minimum value at each step. If starting from the [0][0], it's hard to make decision which direction to go next!
--------------------------------------- problems ------------------------------------------------------------------------
the initialization of min_val[m-1] should be right considered!!
min_val[m-1] = max(1, 1 - dungeon[m-1][n-1])
--------------------------------------- codes -----------------------------------------------------------------------------

class Solution {
public:
    int calculateMinimumHP(vector<vector<int> > &dungeon) {
        //DP problem
        //get the maximum sum of the bottom-right grid
        if (dungeon.size() == 0) {
            return 1;
        }
        int m = dungeon[0].size();
        int n = dungeon.size();
        
        int i, j;
        vector<int> min_val(dungeon[n-1]);
        //bug here before
        min_val[m-1] = max(1, 1 - dungeon[n-1][m-1]);
        for (i = m - 2; i >= 0; i--) {
            min_val[i] = max(1, min_val[i+1] - dungeon[n-1][i]);
        }
        for (j = n - 2; j >= 0; j--) {
            min_val[m-1] = max(1, min_val[m-1] - dungeon[j][m-1]);
            for (i = m - 2; i >= 0; i--) {
                // here if we change to the codes below, the run time will improve a lot!
                //  int tmp = min_val[i] < min_val[i+1]?min_val[i]:min_val[i+1];
                //  min_val[i] = max(1, tmp - dungeon[j][i]);
                min_val[i] = max(1, min(min_val[i], min_val[i+1]) - dungeon[j][i]);
            }
        }
        return min_val[0];
    }
};

No comments:

Post a Comment