-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanyoncrossing.cpp
80 lines (69 loc) · 1.71 KB
/
canyoncrossing.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
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ii = pair<ll, ll>;
using vi = vector<ll>;
using vii = vector<ii>;
vector<vi> M;
ll r,c,k;
int a[] = {0,1, 0,-1, 1,0,-1,0};
int helper(ll m) {
vector<vi> visited; visited.assign(r+1, vi(c+1,0));
queue<ii> q[k+2];
for (int i=1; i<=c; i++) {
visited[r][i] = 1;
if (M[r][i] < m) {
q[1].emplace(r, i);
} else {
q[0].emplace(r, i);
}
}
for (int i=0; i<=k; i++) {
while (!q[i].empty()) {
ii u = q[i].front(); q[i].pop();
if (u.first == 1) return 1;
for (int j=0; j<=6; j+=2) {
ll rr = u.first + a[j];
ll cc = u.second + a[j+1];
if (rr < 1 || cc < 1) continue;
if (rr >= r || cc > c) continue;
if (visited[rr][cc]) continue;
visited[rr][cc] = 1;
if (M[rr][cc] < m) {
q[i+1].emplace(rr,cc);
} else {
q[i].emplace(rr,cc);
}
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("1.in","r",stdin);
cin >> r >> c >> k;
ll maxv = 0;
M.assign(r+1, vi(c+1,0));
for (int i=1; i<=r; i++) {
for (int j=1; j<=c; j++) {
cin >> M[i][j];
maxv = max(maxv, M[i][j]);
}
}
ll L = 0;
ll R = maxv;
ll ans = maxv;
while (L <= R) {
ll m = (L+R)/2;
if (helper(m)) {
ans = m;
L = m + 1;
} else {
R = m - 1;
}
}
cout << ans;
}