/
dungeon-game.cpp
52 lines (43 loc) · 1.18 KB
/
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <vector>
#include <iostream>
using namespace std;
class Solution
{
public:
int calculateMinimumHP(vector<vector<int> > &dungeon)
{
int m = dungeon.size();
if (m == 0) {
return 1;
}
int n = dungeon[0].size();
if (n == 0) {
return 1;
}
vector<vector<int> > f(m, vector<int> (n, INT_MAX));
f[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) {
int cost = max(1, f[i + 1][j] - dungeon[i][j]);
f[i][j] = min(f[i][j], cost);
}
if (j != n - 1) {
int cost = max(1, f[i][j + 1] - dungeon[i][j]);
f[i][j] = min(f[i][j], cost);
}
}
}
return f[0][0];
}
};
int main(int argc, char *argv[])
{
vector<vector<int> > dungeon(3, vector<int> (3));
dungeon[0] = {-2, -3, 3};
dungeon[1] = {-5, -10, 1};
dungeon[2] = {10, 30, -5};
Solution solver;
cout << solver.calculateMinimumHP(dungeon) << endl;
return 0;
}