地下城游戏
![在这里插入图片描述](https://img-blog.csdnimg.cn/201907102334215.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9ob3V0YW8=,size_16,color_FFFFFF,t_70)
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
//这里需要向后递推过程的,从右下角转到左上角
vector<vector<int>> dp = dungeon;
int col = dungeon.size();
int row = dungeon[0].size();
for (int i = col - 1; i >= 0; --i)
{
for (int j = row - 1; j >= 0; --j)
{
if (i == (col - 1) && (j == row - 1))//如果是最后一个数字
{
dp[i][j] = max(1, 1 - dungeon[i][j]);
}
else if (i == col - 1)
dp[i][j] = max(1, dp[i][j + 1] - dungeon[i][j]);
else if (j == row - 1)
dp[i][j] = max(1, dp[i+1][j] - dungeon[i][j]);
else
{
dp[i][j] = min(max(1, dp[i][j + 1] - dungeon[i][j]), dp[i][j] = max(1, dp[i + 1][j] - dungeon[i][j]));
}
}
}
return dp[0][0];
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190710233434735.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3poYW9ob3V0YW8=,size_16,color_FFFFFF,t_70)