#include <bits/stdc++.h>
using namespace std;
int main() {
int n, s; cin >> n >> s;
vector<vector<char>> g(n, vector<char>(n));
for(auto &i : g) for(auto &j : i) cin >> j;
queue<array<int, 3>> q;
pair<int, int> start;
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){
if(g[i][j] == 'M') start = {i, j};
if(g[i][j] == 'H') q.push({i, j, 0});
}
vector<vector<int>> free(n, vector<int>(n, 1e9));
while(!q.empty()){
auto [x, y, d] = q.front(); q.pop();
if(x < 0 || y < 0 || x >= n || y >= n) continue;
if(g[x][y] == 'D' || g[x][y] == 'T') continue;
if(free[x][y] != 1e9) continue;
free[x][y] = d;
q.push({x+1, y, d+1});
q.push({x-1, y, d+1});
q.push({x, y+1, d+1});
q.push({x, y-1, d+1});
}
int l = -1, r = 1e8;
while(l < r-1){
int m = (l+r)/2;
bool ok = 0;
vector<vector<bool>> vis(n, vector<bool>(n));
queue<array<int, 4>> b; b.push({start.first, start.second, m, s});
while(!b.empty()){
auto [x, y, d, st] = b.front(); b.pop();
if(x < 0 || y < 0 || x >= n || y >= n) continue;
if(st == 0){
st = s;
d++;
}
if(vis[x][y]) continue; vis[x][y] = 1;
if(g[x][y] == 'T') continue;
if(free[x][y] <= d) continue;
if(g[x][y] == 'D'){
ok = 1;
break;
}
b.push({x+1, y, d, st-1});
b.push({x-1, y, d, st-1});
b.push({x, y+1, d, st-1});
b.push({x, y-1, d, st-1});
}
if(ok) l = m;
else r = m;
}
cout << l;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:48:7: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
48 | if(vis[x][y]) continue; vis[x][y] = 1;
| ^~
mecho.cpp:48:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
48 | if(vis[x][y]) continue; vis[x][y] = 1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
152 ms |
4236 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
10 ms |
1044 KB |
Output is correct |
34 |
Correct |
10 ms |
860 KB |
Output is correct |
35 |
Correct |
32 ms |
1116 KB |
Output is correct |
36 |
Correct |
9 ms |
1112 KB |
Output is correct |
37 |
Correct |
10 ms |
1116 KB |
Output is correct |
38 |
Correct |
45 ms |
1116 KB |
Output is correct |
39 |
Correct |
11 ms |
1368 KB |
Output is correct |
40 |
Correct |
12 ms |
1368 KB |
Output is correct |
41 |
Correct |
57 ms |
1372 KB |
Output is correct |
42 |
Correct |
15 ms |
1624 KB |
Output is correct |
43 |
Correct |
18 ms |
1628 KB |
Output is correct |
44 |
Correct |
77 ms |
1780 KB |
Output is correct |
45 |
Correct |
17 ms |
1880 KB |
Output is correct |
46 |
Correct |
20 ms |
1884 KB |
Output is correct |
47 |
Correct |
86 ms |
2032 KB |
Output is correct |
48 |
Correct |
20 ms |
2316 KB |
Output is correct |
49 |
Correct |
21 ms |
2140 KB |
Output is correct |
50 |
Correct |
110 ms |
2388 KB |
Output is correct |
51 |
Correct |
23 ms |
2392 KB |
Output is correct |
52 |
Correct |
23 ms |
2392 KB |
Output is correct |
53 |
Correct |
122 ms |
2656 KB |
Output is correct |
54 |
Correct |
29 ms |
2868 KB |
Output is correct |
55 |
Correct |
28 ms |
2736 KB |
Output is correct |
56 |
Correct |
141 ms |
3012 KB |
Output is correct |
57 |
Correct |
31 ms |
3164 KB |
Output is correct |
58 |
Correct |
32 ms |
3224 KB |
Output is correct |
59 |
Correct |
176 ms |
3312 KB |
Output is correct |
60 |
Correct |
35 ms |
3664 KB |
Output is correct |
61 |
Correct |
35 ms |
3664 KB |
Output is correct |
62 |
Correct |
199 ms |
3628 KB |
Output is correct |
63 |
Correct |
136 ms |
3676 KB |
Output is correct |
64 |
Correct |
198 ms |
3896 KB |
Output is correct |
65 |
Correct |
206 ms |
3736 KB |
Output is correct |
66 |
Correct |
157 ms |
3744 KB |
Output is correct |
67 |
Correct |
164 ms |
3896 KB |
Output is correct |
68 |
Correct |
67 ms |
3784 KB |
Output is correct |
69 |
Correct |
70 ms |
3668 KB |
Output is correct |
70 |
Correct |
67 ms |
3632 KB |
Output is correct |
71 |
Correct |
56 ms |
3676 KB |
Output is correct |
72 |
Correct |
50 ms |
3752 KB |
Output is correct |
73 |
Correct |
59 ms |
5212 KB |
Output is correct |
74 |
Correct |
107 ms |
5256 KB |
Output is correct |
75 |
Correct |
113 ms |
5200 KB |
Output is correct |
76 |
Correct |
112 ms |
5200 KB |
Output is correct |
77 |
Correct |
115 ms |
5212 KB |
Output is correct |
78 |
Correct |
128 ms |
4948 KB |
Output is correct |
79 |
Correct |
106 ms |
5088 KB |
Output is correct |
80 |
Correct |
108 ms |
5012 KB |
Output is correct |
81 |
Correct |
125 ms |
4912 KB |
Output is correct |
82 |
Correct |
106 ms |
5160 KB |
Output is correct |
83 |
Correct |
132 ms |
4832 KB |
Output is correct |
84 |
Correct |
117 ms |
4832 KB |
Output is correct |
85 |
Correct |
114 ms |
4832 KB |
Output is correct |
86 |
Correct |
140 ms |
4668 KB |
Output is correct |
87 |
Correct |
147 ms |
4700 KB |
Output is correct |
88 |
Correct |
128 ms |
4408 KB |
Output is correct |
89 |
Correct |
138 ms |
4432 KB |
Output is correct |
90 |
Correct |
162 ms |
4564 KB |
Output is correct |
91 |
Correct |
147 ms |
4436 KB |
Output is correct |
92 |
Correct |
145 ms |
4572 KB |
Output is correct |