Leek-- 174. Dungeon Game

Topic Link: 174. Dungeon Game - LeetCode 

The following is the process of solving this problem with the idea of ​​​​dynamic programming . I believe that all of you can understand and master this classic dynamic programming problem.

Reference Code:

class Solution {
public:
    int calculateMinimumHP(vector<vector<int>>& dungeon) {
        int m=dungeon.size();
        int n=dungeon[0].size();
        //防止超出正数范围的一个技巧,足够大又不会超出最大整数,是最大整数的一半
        int INF=0x3f3f3f3f;
        //多开一行和一列
        vector<vector<int>> dp(m+1,vector<int>(n+1,INF));
        //初始化两个特殊位置
        dp[m-1][n]=dp[m][n-1]=1;
        //从下往上,从右往左填表
        for(int i=m-1;i>=0;i--)
        {
            for(int j=n-1;j>=0;j--)
            {
                dp[i][j]=min(dp[i][j+1],dp[i+1][j])-dungeon[i][j];
                //处理特殊情况
                dp[i][j]=max(dp[i][j],1);
            }
        }
        return dp[0][0];
    }
};

 The above is the whole process of analyzing this dp question, have you learned it yet? If the above solutions are helpful to you, then please be careful and pay attention to it. We will continue to update the classic questions of dynamic programming in the future. See you in the next issue! ! ! ! ! ! ! ! !

Guess you like

Origin blog.csdn.net/weixin_70056514/article/details/131513364