-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdungeon-game.cpp
72 lines (54 loc) · 1.39 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <bits/stdc++.h>
using namespace std;
const int di[] = {0, 1};
const int dj[] = {1, 0};
class Solution {
int n, m;
vector<vector<int>> dungeon;
int healths[201][201];
bool visited[201][201];
bool possible(const int health) {
memset(healths, 0, sizeof healths);
memset(visited, 0, sizeof visited);
healths[0][0] = health + dungeon.at(0).at(0);
visited[0][0] = true;
queue<pair<int, int>> q;
q.emplace(0, 0);
while (!q.empty()) {
const auto [i, j] = q.front();
q.pop();
const int h = healths[i][j];
if (h <= 0) continue;
if (i == n - 1 && j == m - 1) return true;
for (int d = 0; d < 2; d++) {
const int i2 = i + di[d];
const int j2 = j + dj[d];
if (i2 >= n || j2 >= m) continue;
const int h2 = h + dungeon[i2][j2];
if (h2 <= 0) continue;
healths[i2][j2] = max(healths[i2][j2], h2);
if (visited[i2][j2]) continue;
q.emplace(i2, j2);
visited[i2][j2] = true;
}
}
return false;
}
public:
int calculateMinimumHP(vector<vector<int>>& dungeon) {
this->dungeon = dungeon;
n = dungeon.size();
m = dungeon.front().size();
int lo = 1;
int hi = 10000;
while (lo < hi) {
const int mid = (lo + hi) / 2;
if (possible(mid)) {
hi = mid;
} else {
lo = mid + 1;
}
}
return hi;
}
};