forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path174.Dungeon-Game.cpp
33 lines (25 loc) · 929 Bytes
/
174.Dungeon-Game.cpp
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
31
32
33
class Solution {
public:
int calculateMinimumHP(vector<vector<int>>& dungeon)
{
int M = dungeon.size();
int N = dungeon[0].size();
auto dp = vector<vector<int>>(M,vector<int>(N,1));
for (int i=M-1; i>=0; i--)
for (int j=N-1; j>=0; j--)
{
if (i==M-1 && j==N-1)
dp[M-1][N-1] = 1;
else if (i==M-1)
dp[i][j] = dp[i][j+1]-dungeon[i][j+1];
else if (j==N-1)
dp[i][j] = dp[i+1][j]-dungeon[i+1][j];
else
dp[i][j] = min( dp[i][j+1]-dungeon[i][j+1], dp[i+1][j]-dungeon[i+1][j] );
dp[i][j] = max(dp[i][j], 1);
}
dp[0][0] = dp[0][0]-dungeon[0][0];
dp[0][0] = max(dp[0][0], 1);
return dp[0][0];
}
};