题目来源
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
.
【Dungeon|Dungeon Game】-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的方法,但是想了半天,想不到怎么用DP。然后我直接遍历,代码如下:
class Solution {
public:
int calculateMinimumHP(vector>& dungeon) {
int m = dungeon.size();
int n = dungeon[0].size();
int res = 0;
int s = 0;
return travel(0, 0, m-1, n-1, res, s, dungeon) + 1;
}int travel(int i, int j, int m, int n, int res, int s, vector>& dungeon)
{
if (dungeon[i][j] >= 0)
s += dungeon[i][j];
else {
s += dungeon[i][j];
if (s < 0) {
res -= s;
s = 0;
}
}
if (i == m && j == n)
return res;
if (i + 1 <= m && j + 1 <= n)
return min(travel(i+1, j, m, n, res, s, dungeon), travel(i, j+1, m, n, res, s, dungeon));
if (i + 1 <= m)
return travel(i+1, j, m, n, res, s, dungeon);
if (j + 1 <= n)
return travel(i, j+1, m, n, res, s, dungeon);
}
};
然后不出意料的超时了,TLE。那应该还是得用DP的方法,再想想。
剪枝试试,代码如下:
class Solution {
public:
int minR = INT_MAX;
int calculateMinimumHP(vector>& dungeon) {
int m = dungeon.size();
int n = dungeon[0].size();
int res = 0;
int s = 0;
return travel(0, 0, m-1, n-1, res, s, dungeon) + 1;
}int travel(int i, int j, int m, int n, int res, int s, vector>& dungeon)
{
if (dungeon[i][j] >= 0)
s += dungeon[i][j];
else {
s += dungeon[i][j];
if (s < 0) {
res -= s;
if (res > minR)
return res;
s = 0;
}
}
if (i == m && j == n) {
if (res < minR)
minR = res;
return res;
}
if (i + 1 <= m && j + 1 <= n)
return min(travel(i+1, j, m, n, res, s, dungeon), travel(i, j+1, m, n, res, s, dungeon));
if (i + 1 <= m)
return travel(i+1, j, m, n, res, s, dungeon);
if (j + 1 <= n)
return travel(i, j+1, m, n, res, s, dungeon);
}
};
还是不行。最后还是看了答案。
class Solution {
public:
int calculateMinimumHP(vector>& dungeon) {
int m = dungeon.size();
int n = dungeon[0].size();
vector> dp(m+1, vector(n+1, INT_MAX));
dp[m][n-1] = dp[m-1][n] = 1;
for (int i=m-1;
i>=0;
i--)
for (int j=n-1;
j>=0;
j--) {
int need = min(dp[i+1][j], dp[i][j+1]) - dungeon[i][j];
dp[i][j] = need <= 0 ? 1 : need;
}
return dp[0][0];
}
};
果然是DP,从后面开始往前算,我都忘了可以这么做,存储的是从当前位置走到末尾需要多少血,然后dp[0][0]就是我们要求的。很漂亮的解法,完全没想到。