#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int32_t main() {
int n, s;
cin >> n >> s;
vector<vector<char>> m(n, vector<char> (n));
int xs, ys;
int xe, ye;
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
cin >> m[i][j];
if (m[i][j] == 'M') xs = i, ys = j;
if (m[i][j] == 'D') xe = i, ye = j;
}
queue<pair<int, int>> Q;
vector<vector<int>> d(n, vector<int> (n, 1e9));
for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
if (m[i][j] == 'H') {
d[i][j] = 0;
Q.push({i, j});
}
}
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
if (x && (m[x-1][y ] == 'G' || m[x-1][y ] == 'M') && d[x-1][y ] > d[x][y] + 1) d[x-1][y ] = d[x][y] + 1, Q.push({x-1, y });
if (y && (m[x ][y-1] == 'G' || m[x ][y-1] == 'M') && d[x ][y-1] > d[x][y] + 1) d[x ][y-1] = d[x][y] + 1, Q.push({x , y-1});
if (x<n-1 && (m[x+1][y ] == 'G' || m[x+1][y ] == 'M') && d[x+1][y ] > d[x][y] + 1) d[x+1][y ] = d[x][y] + 1, Q.push({x+1, y });
if (y<n-1 && (m[x ][y+1] == 'G' || m[x ][y+1] == 'M') && d[x ][y+1] > d[x][y] + 1) d[x ][y+1] = d[x][y] + 1, Q.push({x , y+1});
}
auto psb = [&](int delay) {
// bfs from mecho's location
// it's possible iff dist_from_mecho[i][j]/s + delay < d[i][j]
queue<pair<int, int>> Q;
vector<vector<int>> dm(n, vector<int> (n, 1e9));
Q.push({xs, ys});
dm[xs][ys] = 0;
while (!Q.empty()) {
auto [x, y] = Q.front();
Q.pop();
if (delay + (dm[x][y]) / s >= d[x][y]) continue;
if (x && m[x-1][y ] != 'T' && dm[x-1][y ] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x-1][y ]) dm[x-1][y ] = dm[x][y] + 1, Q.push({x-1, y });
if (y && m[x ][y-1] != 'T' && dm[x ][y-1] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x ][y-1]) dm[x ][y-1] = dm[x][y] + 1, Q.push({x , y-1});
if (x<n-1 && m[x+1][y ] != 'T' && dm[x+1][y ] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x+1][y ]) dm[x+1][y ] = dm[x][y] + 1, Q.push({x+1, y });
if (y<n-1 && m[x ][y+1] != 'T' && dm[x ][y+1] > dm[x][y] + 1 && delay + (dm[x][y] + 1) / s < d[x ][y+1]) dm[x ][y+1] = dm[x][y] + 1, Q.push({x , y+1});
}
return dm[xe][ye] != 1e9;
};
int l = 0, r = n * n + 2;
while (r - l > 1) {
int mid = (l + r) / 2;
if (psb(mid))
l = mid;
else
r = mid;
}
cout << (psb(l) ? l : -1) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
121 ms |
6356 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
11 ms |
1400 KB |
Output is correct |
34 |
Correct |
12 ms |
1396 KB |
Output is correct |
35 |
Correct |
26 ms |
1412 KB |
Output is correct |
36 |
Correct |
15 ms |
1804 KB |
Output is correct |
37 |
Correct |
15 ms |
1740 KB |
Output is correct |
38 |
Correct |
34 ms |
1620 KB |
Output is correct |
39 |
Correct |
19 ms |
2104 KB |
Output is correct |
40 |
Correct |
20 ms |
2152 KB |
Output is correct |
41 |
Correct |
48 ms |
2108 KB |
Output is correct |
42 |
Correct |
23 ms |
2532 KB |
Output is correct |
43 |
Correct |
28 ms |
2716 KB |
Output is correct |
44 |
Correct |
57 ms |
2584 KB |
Output is correct |
45 |
Correct |
33 ms |
2988 KB |
Output is correct |
46 |
Correct |
28 ms |
3068 KB |
Output is correct |
47 |
Correct |
66 ms |
3044 KB |
Output is correct |
48 |
Correct |
33 ms |
3548 KB |
Output is correct |
49 |
Correct |
34 ms |
3568 KB |
Output is correct |
50 |
Correct |
97 ms |
3540 KB |
Output is correct |
51 |
Correct |
42 ms |
4068 KB |
Output is correct |
52 |
Correct |
40 ms |
4104 KB |
Output is correct |
53 |
Correct |
96 ms |
4064 KB |
Output is correct |
54 |
Correct |
46 ms |
4732 KB |
Output is correct |
55 |
Correct |
46 ms |
4736 KB |
Output is correct |
56 |
Correct |
150 ms |
4708 KB |
Output is correct |
57 |
Correct |
63 ms |
5368 KB |
Output is correct |
58 |
Correct |
59 ms |
5368 KB |
Output is correct |
59 |
Correct |
136 ms |
5352 KB |
Output is correct |
60 |
Correct |
60 ms |
6056 KB |
Output is correct |
61 |
Correct |
60 ms |
6048 KB |
Output is correct |
62 |
Correct |
162 ms |
6036 KB |
Output is correct |
63 |
Correct |
148 ms |
6188 KB |
Output is correct |
64 |
Correct |
245 ms |
6076 KB |
Output is correct |
65 |
Correct |
196 ms |
6020 KB |
Output is correct |
66 |
Correct |
162 ms |
6084 KB |
Output is correct |
67 |
Correct |
150 ms |
6036 KB |
Output is correct |
68 |
Correct |
86 ms |
6056 KB |
Output is correct |
69 |
Correct |
97 ms |
6032 KB |
Output is correct |
70 |
Correct |
90 ms |
6060 KB |
Output is correct |
71 |
Correct |
79 ms |
6060 KB |
Output is correct |
72 |
Correct |
72 ms |
6068 KB |
Output is correct |
73 |
Correct |
71 ms |
6092 KB |
Output is correct |
74 |
Correct |
99 ms |
6072 KB |
Output is correct |
75 |
Correct |
126 ms |
6088 KB |
Output is correct |
76 |
Correct |
107 ms |
6072 KB |
Output is correct |
77 |
Correct |
122 ms |
6076 KB |
Output is correct |
78 |
Correct |
106 ms |
6080 KB |
Output is correct |
79 |
Correct |
81 ms |
6076 KB |
Output is correct |
80 |
Correct |
93 ms |
6048 KB |
Output is correct |
81 |
Correct |
131 ms |
6060 KB |
Output is correct |
82 |
Correct |
101 ms |
6052 KB |
Output is correct |
83 |
Correct |
106 ms |
6060 KB |
Output is correct |
84 |
Correct |
108 ms |
6084 KB |
Output is correct |
85 |
Correct |
120 ms |
6080 KB |
Output is correct |
86 |
Correct |
122 ms |
6076 KB |
Output is correct |
87 |
Correct |
116 ms |
6080 KB |
Output is correct |
88 |
Correct |
110 ms |
6052 KB |
Output is correct |
89 |
Correct |
151 ms |
6036 KB |
Output is correct |
90 |
Correct |
123 ms |
6068 KB |
Output is correct |
91 |
Correct |
116 ms |
6060 KB |
Output is correct |
92 |
Correct |
118 ms |
6196 KB |
Output is correct |