https://leetcode.com/problems/dungeon-game/
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.
后来又想,那直接在计算出每个格子能得到的最大血量后,回溯回第一格,回溯方式也是按照找最大血量的格往回走,结果也不对,理由跟上面一样。
后来看了别人的解法,才发现,可以直接从后往前走,dp[i][j]记录的是进入[i, j]格前,如果能够到目的地,需要的最小血量。
dp[i][j] = (dungeon[i][j] - Math.min(dp[i+1][j], dp[i][j+1])) > 0? 1: -(dungeon[i][j] - Math.min(dp[i+1][j], dp[i][j+1]))
就是,当前格需要的血量是当前格提供的血量(可能是负数)减去下一格需要的血量,就是剩下的血量,如果剩下的血量大于等于0的话,那么进入当前格前只需要血量1就可以了,如果剩下的血量小于0的话,就需要提供这些血量。
注意,下一格一般有两个选项 (i, j+1) 和 (i+1, j),选择小的那个,当i=m-1或者j=n-1时就只有一个选项了。
最后dp[0][0]就是进入之前需要的最小血量。
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
if(dungeon.length == 0) return 0;
int m = dungeon.length;
int n = dungeon[0].length;
int max = 1;
int[][] dp = new int[m][n];
int i=m-1, j=n-1;
dp[m-1][n-1] = dungeon[m-1][n-1]>=0? 1: -dungeon[m-1][n-1]+1;
for(i=m-1; i>=0; i--){
for(j=n-1; j>=0; j--){
if(i==(m-1) && j==(n-1)) continue;
if(i==(m-1)){
int need = dungeon[i][j]-dp[i][j+1];
dp[i][j] = need>=0 ? 1 : -need;
continue;
}
if(j==(n-1)){
int need = dungeon[i][j]-dp[i+1][j];
dp[i][j] = need>=0 ? 1 : -need;
continue;
}
int need = dungeon[i][j] - Math.min(dp[i][j+1], dp[i+1][j]);
dp[i][j] = need>=0 ? 1 : -need;
}
}
return dp[0][0];
}
}
DP如果求第一个位置的某些值,可以从后往前走,注意考虑到这种思维方式。
注意,这里可以直接用一维DP,或者直接在原来dungeon数组里面操作,就只需要O(n)或者O(1)时间复杂度了。
一维DP代码如下:
public class Solution {
public int calculateMinimumHP(int[][] dungeon) {
if(dungeon.length == 0) return 0;
int m = dungeon.length;
int n = dungeon[0].length;
int max = 1;
int[] dp = new int[n];
int i=m-1, j=n-1;
dp[n-1] = dungeon[m-1][n-1]>=0? 1: -dungeon[m-1][n-1]+1;
for(i=m-1; i>=0; i--){
for(j=n-1; j>=0; j--){
if(i==(m-1) && j==(n-1)) continue;
if(i==(m-1)){
int need = dungeon[i][j]-dp[j+1];
dp[j] = need>=0 ? 1 : -need;
continue;
}
if(j==(n-1)){
int need = dungeon[i][j]-dp[j];
dp[j] = need>=0 ? 1 : -need;
continue;
}
int need = dungeon[i][j] - Math.min(dp[j+1], dp[j]);
dp[j] = need>=0 ? 1 : -need;
}
}
return dp[0];
}
}
这道题还有tag是binary search, 不过没想出来binary search的方法。。。这里有个binary search的代码,有空研究一下:
https://leetcode.com/discuss/25671/a-simple-c-solution-using-binary-search
增加C++代码,这里直接在dungeon上改的,可以定义C++数组,注意C++数组和JAVA定义语句是不一样的:
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int row = dungeon.size();
int col = dungeon[0].size(); //int dp[row][col]; <span style="font-family: 'small Monaco', Menlo, Consolas, 'Courier New', monospace; line-height: 17.142858505249px; white-space: pre; background-color: rgb(238, 238, 238);">memset(dp,</span><span class="number" style="box-sizing: border-box; color: rgb(0, 136, 0); font-family: 'small Monaco', Menlo, Consolas, 'Courier New', monospace; line-height: 17.142858505249px; white-space: pre; background-color: rgb(238, 238, 238);">0</span><span style="font-family: 'small Monaco', Menlo, Consolas, 'Courier New', monospace; line-height: 17.142858505249px; white-space: pre; background-color: rgb(238, 238, 238);">,</span><span class="keyword" style="box-sizing: border-box; font-weight: bold; font-family: 'small Monaco', Menlo, Consolas, 'Courier New', monospace; line-height: 17.142858505249px; white-space: pre; background-color: rgb(238, 238, 238);">sizeof</span><span style="font-family: 'small Monaco', Menlo, Consolas, 'Courier New', monospace; line-height: 17.142858505249px; white-space: pre; background-color: rgb(238, 238, 238);">(dp));</span>
for(int i=row-1; i>=0; i--){
for(int j=col-1; j>=0; j--){
if(i==(row-1) && j==(col-1)) dungeon[i][j] = dungeon[i][j]<0 ? -dungeon[i][j]+1 : 1;
else{
int need;
if(i==(row-1)) need = dungeon[i][j]-dungeon[i][j+1];
else if(j==(col-1)) need = dungeon[i][j] - dungeon[i+1][j];
else need = dungeon[i][j] -min(dungeon[i][j+1], dungeon[i+1][j]);
dungeon[i][j] = need<0? -need: 1;
}
}
}
return dungeon[0][0]; //return dp[0][0]
}
};