forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmillionairemadness.cpp
84 lines (65 loc) · 1.7 KB
/
millionairemadness.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
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
int inf = 1<<30;
vector<int> dx = {1,-1,0,0};
vector<int> dy = {0,0,1,-1};
int n, m;
bool inrange(int nextx, int nexty) {
return nextx >= 0 && nexty >= 0 && nextx < n && nexty < m;
}
struct state {
int x;
int y;
int val;
};
bool operator<(const state& s1, const state& s2) {
if(s1.val == s2.val) {
if(s1.x == s2.x) {
return s1.y < s2.y;
}
return s1.x < s2.x;
}
return s1.val > s2.val;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n >> m;
vector<vector<int>> val;
vector<vector<int>> best;
vector<vector<int>> vis;
val.resize(n,vector<int>(m));
best.resize(n,vector<int>(m,inf));
vis.resize(n,vector<int>(m,false));
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> val[i][j];
}
}
best[0][0] = 0;
priority_queue<state> q;
q.push({0,0,0});
while(!q.empty()) {
int currx = q.top().x;
int curry = q.top().y;
q.pop();
if(vis[currx][curry]) {
continue;
}
vis[currx][curry] = true;
for(int i = 0; i < 4; i++) {
int nextx = currx + dx[i];
int nexty = curry + dy[i];
if(!inrange(nextx,nexty)) {
continue;
}
int nextval = max(0, val[nextx][nexty] - val[currx][curry]);
nextval = max(nextval, best[currx][curry]);
if(best[nextx][nexty] > nextval) {
best[nextx][nexty] = nextval;
q.push({nextx,nexty,nextval});
}
}
}
cout << best[n-1][m-1] << endl;
}