#include<bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main() {
int n, s;
cin >> n >> s;
vector<vector<char>> f(n, vector<char>(n));
queue<pair<int, int>> q;
vector<vector<int>> mt(n, vector<int>(n, INT32_MAX));
pair<int, int> start;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> f[i][j];
if (f[i][j] == 'M') {
start = {i, j};
} else if (f[i][j] == 'H') {
q.emplace(i, j);
mt[i][j] = 0;
}
}
}
auto inside = [&](int i, int j) {
return i >= 0 && i < n && j >= 0 && j < n && f[i][j] != 'T';
};
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (!inside(ni, nj)) continue;
if (mt[i][j] + 1 < mt[ni][nj]) {
mt[ni][nj] = mt[i][j] + 1;
q.emplace(ni, nj);
}
}
}
bool fl = false;
int l = -1, r = 160000;
while (r > l + 1) {
while (!q.empty()) q.pop();
int mid = (l + r) / 2;
vector<vector<int>> ms(n, vector<int>(n, INT32_MAX));
ms[start.first][start.second] = 0;
bool good = false;
q.emplace(start.first, start.second);
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (int k = 0; k < 4; ++k) {
int ni = i + dx[k];
int nj = j + dy[k];
if (!inside(ni, nj)) continue;
if (f[ni][nj] == 'D') {
good = true;
fl = true;
break;
}
int steps = ms[i][j] + 1;
bool possible = mid + (steps / s) < mt[ni][nj];
if (possible && steps < ms[ni][nj]) {
ms[ni][nj] = steps;
q.emplace(ni, nj);
}
}
if (good) break;
}
if (good) {
l = mid;
} else {
r = mid;
}
}
cout << l;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:31:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | auto [i, j] = q.front();
| ^
mecho.cpp:54:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | auto [i, j] = q.front();
| ^
mecho.cpp:44:10: warning: variable 'fl' set but not used [-Wunused-but-set-variable]
44 | bool fl = false;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
101 ms |
6212 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 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 |
9 ms |
1544 KB |
Output is correct |
34 |
Correct |
8 ms |
1532 KB |
Output is correct |
35 |
Correct |
23 ms |
1548 KB |
Output is correct |
36 |
Correct |
10 ms |
1884 KB |
Output is correct |
37 |
Correct |
12 ms |
1756 KB |
Output is correct |
38 |
Correct |
35 ms |
1764 KB |
Output is correct |
39 |
Correct |
15 ms |
2248 KB |
Output is correct |
40 |
Correct |
12 ms |
2252 KB |
Output is correct |
41 |
Correct |
37 ms |
2252 KB |
Output is correct |
42 |
Correct |
15 ms |
2668 KB |
Output is correct |
43 |
Correct |
15 ms |
2680 KB |
Output is correct |
44 |
Correct |
43 ms |
2700 KB |
Output is correct |
45 |
Correct |
20 ms |
3148 KB |
Output is correct |
46 |
Correct |
19 ms |
3180 KB |
Output is correct |
47 |
Correct |
53 ms |
3128 KB |
Output is correct |
48 |
Incorrect |
22 ms |
3712 KB |
Output isn't correct |
49 |
Correct |
22 ms |
3668 KB |
Output is correct |
50 |
Correct |
62 ms |
3708 KB |
Output is correct |
51 |
Incorrect |
26 ms |
4224 KB |
Output isn't correct |
52 |
Correct |
25 ms |
4188 KB |
Output is correct |
53 |
Correct |
84 ms |
4188 KB |
Output is correct |
54 |
Incorrect |
36 ms |
5348 KB |
Output isn't correct |
55 |
Correct |
30 ms |
4868 KB |
Output is correct |
56 |
Correct |
95 ms |
4808 KB |
Output is correct |
57 |
Incorrect |
34 ms |
5492 KB |
Output isn't correct |
58 |
Correct |
37 ms |
5468 KB |
Output is correct |
59 |
Correct |
111 ms |
5456 KB |
Output is correct |
60 |
Incorrect |
38 ms |
6204 KB |
Output isn't correct |
61 |
Correct |
43 ms |
6280 KB |
Output is correct |
62 |
Correct |
127 ms |
6160 KB |
Output is correct |
63 |
Correct |
109 ms |
6168 KB |
Output is correct |
64 |
Correct |
183 ms |
6172 KB |
Output is correct |
65 |
Correct |
159 ms |
6128 KB |
Output is correct |
66 |
Correct |
121 ms |
6164 KB |
Output is correct |
67 |
Correct |
134 ms |
6144 KB |
Output is correct |
68 |
Correct |
64 ms |
6212 KB |
Output is correct |
69 |
Correct |
64 ms |
6192 KB |
Output is correct |
70 |
Correct |
64 ms |
6380 KB |
Output is correct |
71 |
Correct |
60 ms |
6216 KB |
Output is correct |
72 |
Incorrect |
56 ms |
6164 KB |
Output isn't correct |
73 |
Incorrect |
69 ms |
6232 KB |
Output isn't correct |
74 |
Correct |
73 ms |
6224 KB |
Output is correct |
75 |
Correct |
84 ms |
6232 KB |
Output is correct |
76 |
Correct |
76 ms |
6208 KB |
Output is correct |
77 |
Correct |
78 ms |
6200 KB |
Output is correct |
78 |
Correct |
88 ms |
6188 KB |
Output is correct |
79 |
Correct |
65 ms |
6208 KB |
Output is correct |
80 |
Correct |
75 ms |
6216 KB |
Output is correct |
81 |
Correct |
82 ms |
6176 KB |
Output is correct |
82 |
Correct |
80 ms |
6176 KB |
Output is correct |
83 |
Correct |
109 ms |
6188 KB |
Output is correct |
84 |
Correct |
88 ms |
6240 KB |
Output is correct |
85 |
Correct |
98 ms |
6208 KB |
Output is correct |
86 |
Correct |
89 ms |
6188 KB |
Output is correct |
87 |
Correct |
88 ms |
6188 KB |
Output is correct |
88 |
Correct |
103 ms |
6184 KB |
Output is correct |
89 |
Correct |
94 ms |
6224 KB |
Output is correct |
90 |
Correct |
114 ms |
6260 KB |
Output is correct |
91 |
Correct |
97 ms |
6248 KB |
Output is correct |
92 |
Correct |
100 ms |
6212 KB |
Output is correct |