Dungeon Game (GRAPH - DP)

QUESTION

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)

1st TRY

class Solution {

public:

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

        minInitHP = INT_MAX;

        dfs(dungeon,0,0,0,0);

        return minInitHP+1;

    }

    void dfs(vector<vector<int> > &dungeon, int m, int n, int currentHP, int currentMinInitHP)

    {

        currentHP -= dungeon[m][n];

        currentMinInitHP= max(currentMinInitHP,currentHP);

        if(currentMinInitHP>minInitHP) return;

        if(m==dungeon.size()-1 && n==dungeon[0].size()-1) 

        {

            currentHP = min(currentHP,currentMinInitHP);

            return;

        }

        if(m!=dungeon.size()-1) dfs(dungeon,m+1,n,currentHP,currentMinInitHP);

        if(n!=dungeon[0].size()-1) dfs(dungeon,m,n+1,currentHP,currentMinInitHP);

        

    }

private:

    int minInitHP;

};

Result: Time Limit Exceeded

 

2nd TRY

用空间换时间,动态规划

class Solution {

public:

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

        int m = dungeon.size();  

        int n = dungeon[0].size();  

  

        int **dp = new int*[m]; // min HP needed when knight come here

        for(int i=0; i < m; i++)

            dp[i] = new int[n];

        

        dp[0][0] = 0 - dungeon[0][0];for(int i = 1; i < m; i++)

        {

            dp[i][0] = max(dp[i-1][0]-dungeon[i][0], dp[i-1][0]);

        }

        for(int i = 1; i < n; i++)

        {

            dp[0][i] = max(dp[0][i-1]-dungeon[0][i], dp[0][i-1]);

        }

        for(int i = 1; i < m; i++)

        {

            for(int j = 1; j < n; j++)

            {

                dp[i][j]=min(dp[i-1][j],dp[i][j-1])-min(0,dungeon[i][j]);

            }

        }

        return max(1,dp[m-1][n-1]+1);

    }

};

Result: Wrong Answer

Input: [[0,5],[-2,-3]]
Output: 4
Expected: 1

3rd TRY

状态的保存有问题,需要保存两个状态:到当前格在初始时需要的HP,以及到了当前格的HP

所以要从下往上填状态,那么只要保存一个状态,当前格的HP

class Solution {

public:

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

        int m = dungeon.size();  

        int n = dungeon[0].size();  

  

        int **dp = new int*[m]; // min HP needed when knight from here to end

        for(int i=0; i < m; i++)

            dp[i] = new int[n];

        

        dp[m-1][n-1] = max(0 - dungeon[m-1][n-1],0);

        for(int i = m-2; i >= 0; i--)

        {

            dp[i][n-1] = max(dp[i+1][n-1]-dungeon[i][n-1],0);

        }

        for(int i = n-2; i >= 0; i--)

        {

            dp[m-1][i] = max(dp[m-1][i+1]-dungeon[m-1][i],0);

        }

        for(int i = m-2; i >= 0; i--)

        {

            for(int j = n-2; j >= 0; j--)

            {

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

            }

        }

        return dp[0][0]+1;

    }

};

Result: Accepted

你可能感兴趣的:(Graph)