forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDungeon Game.cpp
42 lines (35 loc) · 1.06 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
class Solution {
public:
int solve(int i, int j, int m , int n, vector<vector<int>> &grid)
{
// if we come out of the grid simply return a large value
if(i >= m || j >= n)
return INT_MAX;
// calucate health by the 2 possible ways
int down = solve(i + 1, j, m, n, grid);
int right = solve(i, j + 1, m, n, grid);
// take the min both both
int health = min(down, right);
// we reach the destination when both the sides return INT_MAX
if(health == INT_MAX)
{
health = 1; // both are +ve large integers so min health required = 1
}
int ans = 0;
if(health - grid[i][j] > 0)
{
ans = health - grid[i][j];
}
else
{
ans = 1;
}
return ans;
}
int calculateMinimumHP(vector<vector<int>>& dungeon)
{
int m = dungeon.size();
int n = dungeon[0].size();
return solve(0, 0, m, n, dungeon);
}
};