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.
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
int m=dungeon.size();
int n=dungeon[0].size();
//不是走最多的那一条,而是路径上的最小值最大的那一条,即且最小值>0
//可以原地操作
dungeon[m-1][n-1]=max(-dungeon[m-1][n-1],0);
for(int j=n-2;j>=0;j--) dungeon[m-1][j]=max(dungeon[m-1][j+1]-dungeon[m-1][j],0);
for(int i=m-2;i>=0;i--) dungeon[i][n-1]=max(dungeon[i+1][n-1]-dungeon[i][n-1],0);
for(int i=m-2;i>=0;i--){
for(int j=n-2;j>=0;j--){
dungeon[i][j]=max(min(dungeon[i+1][j],dungeon[i][j+1])-dungeon[i][j],0);
}
}
return dungeon[0][0]+1;
// int dp[m+1][n+1];
// memset(dp,0,sizeof(dp));
// dp[m-1][n-1]=dungeon[m-1][n-1];
// for(int j=n-2;j>=0;j--) dp[m-1][j]=dp[m-1][j+1]+dungeon[m-1][j];
// for(int i=m-2;i>=0;i--) dp[i][n-1]=dp[i+1][n-1]+dungeon[i][n-1];
// //得到dp数组
// for(int i=m-2;i>=0;i--){
// for(int j=n-2;j>=0;j--){
// dp[i][j]=max(dp[i+1][j],dp[i][j+1])+dungeon[i][j];
// }
// }
// printf("val %d\n",dp[m-1][n-1]);
// return dp[0][0]>0?1:-dp[0][0]+1;
}
//直接按照原来的dp未必是最优的路径
//[[3,-20,30],[-3,4,0]]
// void comeBack(int x,int y,int total,int& minV,int dp[][1000],vector<vector<int>>& dungeon){
// //递归终点
// if(x==0 && y==0){
// //存储最小值
// minV=minValue(minV,dp[x][y],dungeon[x][y]);
// return;
// }
// else minV=min(minV,dp[x][y]);
// //判断是否越界
// if(x-1>=0 && dp[x-1][y]==dp[x][y]-dungeon[x][y])
// comeBack(x-1,y,total,minV,dp,dungeon);
// if(y-1>=0 && dp[x][y-1]==dp[x][y]-dungeon[x][y])
// comeBack(x,y-1,total,minV,dp,dungeon);
// }
// int minValue(int a,int b,int c){
// int d=a>b?b:a;
// return d>c?c:d;
// }
};