#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<ll, ll> pl;
typedef pair<ll, pl> pll;
typedef vector<int> vi;
#define pb push_back
#define f first
#define s second
int __i__, __j__;
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define For(i,a,b) for (int i = (a); i < (b); ++i)
#define Fir(i,a) For(i,0,a)
#define Rof(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define Rif(i,a) Rof(i,0,a)
const int NN = 805, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int n, S, dism[NN][NN], disb[NN][NN], mx, my, ex, ey, dir[4][2]{{0, 1}, {0, -1}, {-1, 0}, {1, 0}}, ans;char g[NN][NN];
queue<pi> qb;
bool check(int x, int y){
return 1 <= x && x <= n && 1 <= y && y <= n && g[x][y] != 'T';
}
void bfs(){
while(!qb.empty()){
auto[ux, uy] = qb.front(); qb.pop();
For(d, 0, 4){
int nx = ux + dir[d][0], ny = uy + dir[d][1];
if(check(nx, ny) && g[nx][ny] != 'D'){
if(disb[nx][ny] == INF){
disb[nx][ny] = disb[ux][uy] + 1;
qb.push({nx, ny});
}
}
}
}
}
bool escapable(int delay){
memset(dism, 0x3f, sizeof(dism));
queue<pi> q; q.push({mx, my});dism[mx][my] = 0;
if(delay >= disb[mx][my])return false;
while(!q.empty()){
auto[x, y] = q.front(); q.pop();
For(d, 0, 4){
int nx = x + dir[d][0], ny = y+dir[d][1];
if(check(nx, ny)){
if(dism[nx][ny] == INF){
if(nx == ex && ny == ey){
return true;
}
if( (dism[x][y]+1)/S < disb[nx][ny] - delay){
dism[nx][ny] = dism[x][y] + 1;
q.push({nx, ny});
}
}
}
}
}
return false;
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> S;
memset(disb, 0x3f, sizeof(disb));
For(i, 1, n+1){
For(j, 1, n+1){
cin >> g[i][j];
if(g[i][j] == 'M'){
mx = i; my = j;
}
if(g[i][j] == 'H'){
qb.push({i, j}); disb[i][j] = 0;
}
if(g[i][j] == 'D'){
ex = i; ey = j;
}
}
}
bfs(); disb[ex][ey] = INF;
int lo = 0, hi = 7e5; ans = -1;
while(lo <= hi){
int mid = (lo + hi)/2;
if(escapable(mid)){
ans = mid; lo = mid+1;
}
else{
hi = mid-1;
}
}
cout << ans << endl;
return 0;
}
Compilation message
mecho.cpp: In function 'void bfs()':
mecho.cpp:34:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
34 | auto[ux, uy] = qb.front(); qb.pop();
| ^
mecho.cpp: In function 'bool escapable(int)':
mecho.cpp:52:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
52 | auto[x, y] = q.front(); q.pop();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5332 KB |
Output is correct |
2 |
Correct |
3 ms |
5332 KB |
Output is correct |
3 |
Correct |
3 ms |
5332 KB |
Output is correct |
4 |
Correct |
4 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5332 KB |
Output is correct |
6 |
Correct |
3 ms |
5332 KB |
Output is correct |
7 |
Correct |
61 ms |
6052 KB |
Output is correct |
8 |
Correct |
4 ms |
5332 KB |
Output is correct |
9 |
Correct |
3 ms |
5332 KB |
Output is correct |
10 |
Correct |
3 ms |
5332 KB |
Output is correct |
11 |
Correct |
3 ms |
5332 KB |
Output is correct |
12 |
Correct |
4 ms |
5332 KB |
Output is correct |
13 |
Correct |
3 ms |
5332 KB |
Output is correct |
14 |
Correct |
3 ms |
5332 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
3 ms |
5332 KB |
Output is correct |
17 |
Correct |
3 ms |
5332 KB |
Output is correct |
18 |
Correct |
3 ms |
5332 KB |
Output is correct |
19 |
Correct |
4 ms |
5332 KB |
Output is correct |
20 |
Correct |
4 ms |
5332 KB |
Output is correct |
21 |
Correct |
3 ms |
5332 KB |
Output is correct |
22 |
Correct |
3 ms |
5332 KB |
Output is correct |
23 |
Correct |
4 ms |
5332 KB |
Output is correct |
24 |
Correct |
3 ms |
5444 KB |
Output is correct |
25 |
Correct |
3 ms |
5332 KB |
Output is correct |
26 |
Correct |
3 ms |
5332 KB |
Output is correct |
27 |
Correct |
3 ms |
5332 KB |
Output is correct |
28 |
Correct |
3 ms |
5332 KB |
Output is correct |
29 |
Correct |
3 ms |
5460 KB |
Output is correct |
30 |
Correct |
3 ms |
5332 KB |
Output is correct |
31 |
Correct |
3 ms |
5436 KB |
Output is correct |
32 |
Correct |
3 ms |
5460 KB |
Output is correct |
33 |
Correct |
5 ms |
5580 KB |
Output is correct |
34 |
Correct |
5 ms |
5588 KB |
Output is correct |
35 |
Correct |
19 ms |
5588 KB |
Output is correct |
36 |
Correct |
6 ms |
5716 KB |
Output is correct |
37 |
Correct |
7 ms |
5712 KB |
Output is correct |
38 |
Correct |
26 ms |
5696 KB |
Output is correct |
39 |
Correct |
6 ms |
5716 KB |
Output is correct |
40 |
Correct |
7 ms |
5660 KB |
Output is correct |
41 |
Correct |
27 ms |
5716 KB |
Output is correct |
42 |
Correct |
7 ms |
5716 KB |
Output is correct |
43 |
Correct |
7 ms |
5776 KB |
Output is correct |
44 |
Correct |
31 ms |
5700 KB |
Output is correct |
45 |
Correct |
9 ms |
5716 KB |
Output is correct |
46 |
Correct |
8 ms |
5736 KB |
Output is correct |
47 |
Correct |
48 ms |
5816 KB |
Output is correct |
48 |
Correct |
9 ms |
5776 KB |
Output is correct |
49 |
Correct |
9 ms |
5844 KB |
Output is correct |
50 |
Correct |
46 ms |
5860 KB |
Output is correct |
51 |
Correct |
10 ms |
5844 KB |
Output is correct |
52 |
Correct |
10 ms |
5844 KB |
Output is correct |
53 |
Correct |
51 ms |
5844 KB |
Output is correct |
54 |
Correct |
11 ms |
5844 KB |
Output is correct |
55 |
Correct |
10 ms |
5844 KB |
Output is correct |
56 |
Correct |
63 ms |
5940 KB |
Output is correct |
57 |
Correct |
12 ms |
5908 KB |
Output is correct |
58 |
Correct |
12 ms |
5892 KB |
Output is correct |
59 |
Correct |
72 ms |
5980 KB |
Output is correct |
60 |
Correct |
14 ms |
5972 KB |
Output is correct |
61 |
Correct |
13 ms |
6016 KB |
Output is correct |
62 |
Correct |
83 ms |
6020 KB |
Output is correct |
63 |
Correct |
77 ms |
6008 KB |
Output is correct |
64 |
Correct |
128 ms |
5896 KB |
Output is correct |
65 |
Correct |
130 ms |
6008 KB |
Output is correct |
66 |
Correct |
103 ms |
5940 KB |
Output is correct |
67 |
Correct |
88 ms |
5928 KB |
Output is correct |
68 |
Correct |
36 ms |
5956 KB |
Output is correct |
69 |
Correct |
33 ms |
6056 KB |
Output is correct |
70 |
Correct |
37 ms |
6036 KB |
Output is correct |
71 |
Correct |
29 ms |
6060 KB |
Output is correct |
72 |
Correct |
24 ms |
5964 KB |
Output is correct |
73 |
Correct |
26 ms |
6288 KB |
Output is correct |
74 |
Correct |
44 ms |
6300 KB |
Output is correct |
75 |
Correct |
48 ms |
6212 KB |
Output is correct |
76 |
Correct |
44 ms |
6292 KB |
Output is correct |
77 |
Correct |
47 ms |
6228 KB |
Output is correct |
78 |
Correct |
54 ms |
6264 KB |
Output is correct |
79 |
Correct |
42 ms |
6268 KB |
Output is correct |
80 |
Correct |
46 ms |
6188 KB |
Output is correct |
81 |
Correct |
51 ms |
6188 KB |
Output is correct |
82 |
Correct |
45 ms |
6228 KB |
Output is correct |
83 |
Correct |
55 ms |
6220 KB |
Output is correct |
84 |
Correct |
55 ms |
6224 KB |
Output is correct |
85 |
Correct |
50 ms |
6144 KB |
Output is correct |
86 |
Correct |
58 ms |
6228 KB |
Output is correct |
87 |
Correct |
57 ms |
6148 KB |
Output is correct |
88 |
Correct |
59 ms |
6072 KB |
Output is correct |
89 |
Correct |
59 ms |
6168 KB |
Output is correct |
90 |
Correct |
64 ms |
6092 KB |
Output is correct |
91 |
Correct |
58 ms |
6172 KB |
Output is correct |
92 |
Correct |
58 ms |
6096 KB |
Output is correct |