#include<bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
const int max_n = 800;
char f[max_n][max_n];
int mt[max_n][max_n];
int n, s;
pair<int, int> start;
bool inside(int i, int j) {
return i >= 0 && i < n && j >= 0 && j < n && f[i][j] != 'T';
}
bool check(int offset) {
queue<pair<int, int>> q;
vector<vector<int>> ms(n, vector<int>(n, 10e7));
ms[start.first][start.second] = 0;
q.emplace(start.first, start.second);
if (mt[start.first][start.second] <= offset) {
return false;
}
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') {
return true;
}
int steps = ms[i][j] + 1;
bool possible = offset + (steps / s) < mt[ni][nj];
if (possible && steps < ms[ni][nj]) {
ms[ni][nj] = steps;
q.emplace(ni, nj);
}
}
}
return false;
}
int main() {
cin >> n >> s;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
mt[i][j] = 10e7;
}
}
queue<pair<int, int>> q;
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;
}
}
}
// make mt
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) || f[ni][nj] == 'D') continue;
if (mt[i][j] + 1 < mt[ni][nj]) {
mt[ni][nj] = mt[i][j] + 1;
q.emplace(ni, nj);
}
}
}
if (!check(0)) {
cout << -1;
return 0;
}
int l = 0, r = 160000;
while (r > l + 1) {
int mid = (r + l) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << l;
}
Compilation message
mecho.cpp: In function 'bool check(int)':
mecho.cpp:29:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | auto [i, j] = q.front();
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:71:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
71 | auto [i, j] = q.front();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
140 ms |
6184 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
756 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
2648 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
2648 KB |
Output is correct |
28 |
Correct |
1 ms |
2648 KB |
Output is correct |
29 |
Correct |
1 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2648 KB |
Output is correct |
31 |
Correct |
1 ms |
2648 KB |
Output is correct |
32 |
Correct |
1 ms |
2648 KB |
Output is correct |
33 |
Correct |
9 ms |
3428 KB |
Output is correct |
34 |
Correct |
8 ms |
3428 KB |
Output is correct |
35 |
Correct |
25 ms |
3684 KB |
Output is correct |
36 |
Correct |
10 ms |
3724 KB |
Output is correct |
37 |
Correct |
11 ms |
3624 KB |
Output is correct |
38 |
Correct |
39 ms |
3624 KB |
Output is correct |
39 |
Correct |
12 ms |
3800 KB |
Output is correct |
40 |
Correct |
13 ms |
3828 KB |
Output is correct |
41 |
Correct |
41 ms |
4048 KB |
Output is correct |
42 |
Correct |
15 ms |
4056 KB |
Output is correct |
43 |
Correct |
16 ms |
4072 KB |
Output is correct |
44 |
Correct |
51 ms |
4072 KB |
Output is correct |
45 |
Correct |
20 ms |
4284 KB |
Output is correct |
46 |
Correct |
19 ms |
4284 KB |
Output is correct |
47 |
Correct |
68 ms |
4368 KB |
Output is correct |
48 |
Incorrect |
23 ms |
4616 KB |
Output isn't correct |
49 |
Correct |
23 ms |
4616 KB |
Output is correct |
50 |
Correct |
77 ms |
4552 KB |
Output is correct |
51 |
Incorrect |
26 ms |
4860 KB |
Output isn't correct |
52 |
Correct |
27 ms |
4880 KB |
Output is correct |
53 |
Correct |
94 ms |
4868 KB |
Output is correct |
54 |
Incorrect |
31 ms |
5168 KB |
Output isn't correct |
55 |
Correct |
31 ms |
5168 KB |
Output is correct |
56 |
Correct |
107 ms |
5188 KB |
Output is correct |
57 |
Incorrect |
34 ms |
5624 KB |
Output isn't correct |
58 |
Correct |
35 ms |
5600 KB |
Output is correct |
59 |
Correct |
152 ms |
5584 KB |
Output is correct |
60 |
Incorrect |
39 ms |
6144 KB |
Output isn't correct |
61 |
Correct |
43 ms |
6144 KB |
Output is correct |
62 |
Correct |
154 ms |
6112 KB |
Output is correct |
63 |
Correct |
127 ms |
6132 KB |
Output is correct |
64 |
Correct |
216 ms |
6176 KB |
Output is correct |
65 |
Correct |
161 ms |
6072 KB |
Output is correct |
66 |
Correct |
135 ms |
6120 KB |
Output is correct |
67 |
Correct |
45 ms |
6084 KB |
Output is correct |
68 |
Correct |
68 ms |
6132 KB |
Output is correct |
69 |
Correct |
77 ms |
6148 KB |
Output is correct |
70 |
Correct |
63 ms |
6144 KB |
Output is correct |
71 |
Correct |
63 ms |
6128 KB |
Output is correct |
72 |
Correct |
50 ms |
6144 KB |
Output is correct |
73 |
Correct |
62 ms |
6304 KB |
Output is correct |
74 |
Correct |
84 ms |
6148 KB |
Output is correct |
75 |
Correct |
100 ms |
6112 KB |
Output is correct |
76 |
Correct |
95 ms |
6160 KB |
Output is correct |
77 |
Correct |
103 ms |
6132 KB |
Output is correct |
78 |
Correct |
42 ms |
6120 KB |
Output is correct |
79 |
Correct |
78 ms |
6128 KB |
Output is correct |
80 |
Correct |
88 ms |
6140 KB |
Output is correct |
81 |
Correct |
93 ms |
6128 KB |
Output is correct |
82 |
Correct |
84 ms |
6156 KB |
Output is correct |
83 |
Correct |
108 ms |
6124 KB |
Output is correct |
84 |
Correct |
106 ms |
6124 KB |
Output is correct |
85 |
Correct |
105 ms |
6404 KB |
Output is correct |
86 |
Correct |
111 ms |
6132 KB |
Output is correct |
87 |
Correct |
110 ms |
6184 KB |
Output is correct |
88 |
Correct |
106 ms |
6136 KB |
Output is correct |
89 |
Correct |
120 ms |
6108 KB |
Output is correct |
90 |
Correct |
127 ms |
6240 KB |
Output is correct |
91 |
Correct |
111 ms |
6140 KB |
Output is correct |
92 |
Correct |
107 ms |
6120 KB |
Output is correct |