-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDungeonGame.cc
More file actions
30 lines (25 loc) · 927 Bytes
/
DungeonGame.cc
File metadata and controls
30 lines (25 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int m = dungeon.size();
assert (m > 0);
int n = dungeon[0].size();
assert (n > 0);
vector<vector<int>> dp(m, vector<int>(n, 0));
dp[m - 1][n - 1] = max(1 - dungeon[m - 1][n - 1], 1);
for (int i = m - 2; i >= 0; i--) {
dp[i][n - 1] = max(dp[i + 1][n - 1] - dungeon[i][n - 1], 1);
}
for (int j = n - 2; j >= 0; j--) {
dp[m - 1][j] = max(dp[m - 1][j + 1] - dungeon[m - 1][j], 1);
}
for (int i = m - 2; i >= 0; i--) {
for (int j = n - 2; j >= 0; j--) {
int h1 = max(dp[i + 1][j] - dungeon[i][j], 1);
int h2 = max(dp[i][j + 1] - dungeon[i][j], 1);
dp[i][j] = min(h1, h2);
}
}
return dp[0][0];
}
};