-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdungeongame.cpp
More file actions
40 lines (40 loc) · 1.13 KB
/
dungeongame.cpp
File metadata and controls
40 lines (40 loc) · 1.13 KB
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
34
35
36
37
38
39
40
class Solution {
public:
int calculateMinimumHP(vector<vector<int> > &dungeon) {
int m = dungeon.size();
if (m == 0)
{
return 0;
}
int n = dungeon[0].size();
if (n == 0)
{
return 0;
}
vector<vector<int>>count(m, vector<int>(n,0));
count[m - 1][n - 1] = max(1, 1-dungeon[m - 1][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)
{
continue;
}
else if (i == m - 1)
{
count[i][j] = max(1, count[i][j+1] -dungeon[i][j]);
}
else if (j == n - 1)
{
count[i][j] = max(1, count[i+1][j] - dungeon[i][j]);
}
else
{
count[i][j] = max(1, min(count[i + 1][j], count[i][j+1]) - dungeon[i][j]);
}
}
}
return count[0][0];
}
};