LeetCode - Dungeon Game

Dungeon Game

2015.1.23 19:38

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.

Solution:

  This problem should be one about dynamic programming. Let dp[i][j] be the minimal HP necessary to reach the destination (n - 1, m - 1), if you start from the position (i, j). Then the answer lies in dp[0][0], which will be calculated backward from (n - 1, m - 1) all the way back to (0, 0).

  Try to understand the meaning of min(), even if you can pass a position (i, j), you'll always need at least 1 point of HP to stay alive.

  Time and space complexity are both O(n * m), though space can be reduced to O(m) with extra coding, you know how, right?

Accepted code:

 1 class Solution {

 2 public:

 3     int calculateMinimumHP(vector<vector<int> > &dungeon) {

 4         int n, m;

 5         

 6         n = (int)dungeon.size();

 7         m = (int)dungeon[0].size();

 8         

 9         int i, j;

10         vector<vector<int> > dp;

11         

12         dp.resize(n, vector<int>(m));

13         dp[n - 1][m - 1] = min(1 - dungeon[n - 1][m - 1]);

14         for (i = n - 2; i >= 0; --i) {

15             dp[i][m - 1] = min(dp[i + 1][m - 1] - dungeon[i][m - 1]);

16         }

17         for (j = m - 2; j >= 0; --j) {

18             dp[n - 1][j] = min(dp[n - 1][j + 1] - dungeon[n - 1][j]);

19         }

20         

21         int s1, s2;

22         for (i = n - 2; i >= 0; --i) {

23             for (j = m - 2; j >= 0; --j) {

24                 s1 = dp[i][j + 1];

25                 s2 = dp[i + 1][j];

26                 dp[i][j] = min((s1 < s2 ? s1 : s2) - dungeon[i][j]);

27             }

28         }

29         s1 = dp[0][0];

30         for (i = 0; i < n; ++i) {

31             dp[i].clear();

32         }

33         dp.clear();

34         

35         return s1;

36     }

37 private:

38     int min(int x) {

39         return x <= 0 ? 1 : x;

40     }

41 };

你可能感兴趣的:(LeetCode)