#include <bits/stdc++.h>
#define pii pair<int, int>
#define ll long long
#define ld long double
using namespace std;
vector<string> grid;
int n, s;
void expand(queue<pii>& bees, vector<string>& mat, vector<vector<bool> > &expanded ) {
queue<pii> newque;
while (!bees.empty()) {
pii curr = bees.front();
bees.pop();
if (expanded[curr.first][curr.second])
continue;
if (mat[curr.first + 1][curr.second] == 'G' || mat[curr.first + 1][curr.second] == 'M')
mat[curr.first + 1][curr.second] = 'H', newque.push({curr.first + 1, curr.second});
if (mat[curr.first - 1][curr.second] == 'G' || mat[curr.first - 1][curr.second] == 'M')
mat[curr.first - 1][curr.second] = 'H', newque.push({curr.first - 1, curr.second});
if (mat[curr.first][curr.second + 1] == 'G' || mat[curr.first][curr.second + 1] == 'M')
mat[curr.first][curr.second + 1] = 'H', newque.push({curr.first, curr.second + 1});
if (mat[curr.first][curr.second - 1] == 'G' || mat[curr.first][curr.second - 1] == 'M')
mat[curr.first][curr.second - 1] = 'H', newque.push({curr.first, curr.second - 1});
expanded[curr.first][curr.second] = true;
}
bees = newque;
}
bool possible(int wait, queue<pii> bees, pii start, pii endd) {
vector<vector<bool> > expanded(n + 2, vector<bool>(n + 2, false));
vector<string> mat = grid;
mat[start.first][start.second] = 'G';
for (int i = 0; i < wait; i++)
expand(bees, mat, expanded);
vector<vector<bool> > visited(n + 2, vector<bool>(n + 2, false));
int tim = s;
queue<pair<int, pii> > que;
que.push({0, {start.first, start.second}});
while (!que.empty())
{
pair<int, pii> curr = que.front();
que.pop();
if (curr.first == tim) {
expand(bees, mat, expanded);
tim += s;
}
if (!(mat[curr.second.first][curr.second.second] == 'G' || mat[curr.second.first][curr.second.second] == 'D')
|| visited[curr.second.first][curr.second.second])
continue;
visited[curr.second.first][curr.second.second] = true;
que.push({curr.first + 1, {curr.second.first + 1, curr.second.second}});
que.push({curr.first + 1, {curr.second.first - 1, curr.second.second}});
que.push({curr.first + 1, {curr.second.first, curr.second.second + 1}});
que.push({curr.first + 1, {curr.second.first, curr.second.second - 1}});
}
return visited[endd.first][endd.second];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
grid.resize(n + 2);
string wall;
for (int i = 0; i < n + 2; i++)
wall += 'T';
grid[0] = wall, grid[n + 1] = wall;
for (int i = 1; i <= n; i++) {
string curr = "T";
string inp;
cin >> inp;
curr += inp;
curr += 'T';
grid[i] = curr;
}
queue<pii> bees;
pii start, endd;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (grid[i][j] == 'D')
endd = {i, j};
else if (grid[i][j] == 'H')
bees.push({i, j});
else if (grid[i][j] == 'M')
start = {i, j};
}
}
int l = 0, r = 10 * n;
while (l < r) {
int mid = (l + r) / 2;
if (possible(mid, bees, start, endd))
l = 1 + mid;
else
r = mid;
}
cout << l - 1 << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
248 ms |
2252 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Incorrect |
1 ms |
320 KB |
Output isn't correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Incorrect |
1 ms |
324 KB |
Output isn't correct |
22 |
Correct |
2 ms |
212 KB |
Output is correct |
23 |
Incorrect |
2 ms |
316 KB |
Output isn't correct |
24 |
Incorrect |
2 ms |
212 KB |
Output isn't correct |
25 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
27 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
28 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
29 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
30 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
31 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
340 KB |
Output isn't correct |
33 |
Incorrect |
48 ms |
768 KB |
Output isn't correct |
34 |
Incorrect |
33 ms |
772 KB |
Output isn't correct |
35 |
Correct |
41 ms |
724 KB |
Output is correct |
36 |
Incorrect |
60 ms |
852 KB |
Output isn't correct |
37 |
Incorrect |
42 ms |
892 KB |
Output isn't correct |
38 |
Correct |
57 ms |
912 KB |
Output is correct |
39 |
Incorrect |
82 ms |
984 KB |
Output isn't correct |
40 |
Incorrect |
58 ms |
1004 KB |
Output isn't correct |
41 |
Correct |
68 ms |
1012 KB |
Output is correct |
42 |
Incorrect |
100 ms |
1100 KB |
Output isn't correct |
43 |
Incorrect |
73 ms |
1228 KB |
Output isn't correct |
44 |
Correct |
98 ms |
1128 KB |
Output is correct |
45 |
Incorrect |
125 ms |
1236 KB |
Output isn't correct |
46 |
Incorrect |
91 ms |
1252 KB |
Output isn't correct |
47 |
Correct |
104 ms |
1268 KB |
Output is correct |
48 |
Incorrect |
151 ms |
1392 KB |
Output isn't correct |
49 |
Incorrect |
104 ms |
1384 KB |
Output isn't correct |
50 |
Correct |
122 ms |
1364 KB |
Output is correct |
51 |
Incorrect |
168 ms |
1492 KB |
Output isn't correct |
52 |
Incorrect |
118 ms |
1540 KB |
Output isn't correct |
53 |
Correct |
156 ms |
1552 KB |
Output is correct |
54 |
Incorrect |
204 ms |
1680 KB |
Output isn't correct |
55 |
Incorrect |
141 ms |
1672 KB |
Output isn't correct |
56 |
Correct |
194 ms |
1716 KB |
Output is correct |
57 |
Incorrect |
231 ms |
1860 KB |
Output isn't correct |
58 |
Incorrect |
156 ms |
1860 KB |
Output isn't correct |
59 |
Correct |
210 ms |
1860 KB |
Output is correct |
60 |
Incorrect |
256 ms |
2040 KB |
Output isn't correct |
61 |
Incorrect |
189 ms |
1996 KB |
Output isn't correct |
62 |
Correct |
238 ms |
1956 KB |
Output is correct |
63 |
Correct |
196 ms |
1996 KB |
Output is correct |
64 |
Correct |
259 ms |
2052 KB |
Output is correct |
65 |
Correct |
257 ms |
2124 KB |
Output is correct |
66 |
Correct |
237 ms |
2052 KB |
Output is correct |
67 |
Correct |
217 ms |
2052 KB |
Output is correct |
68 |
Correct |
151 ms |
1996 KB |
Output is correct |
69 |
Correct |
126 ms |
2004 KB |
Output is correct |
70 |
Correct |
142 ms |
2036 KB |
Output is correct |
71 |
Correct |
114 ms |
2032 KB |
Output is correct |
72 |
Correct |
165 ms |
2040 KB |
Output is correct |
73 |
Correct |
137 ms |
2580 KB |
Output is correct |
74 |
Correct |
220 ms |
2500 KB |
Output is correct |
75 |
Correct |
243 ms |
2476 KB |
Output is correct |
76 |
Correct |
229 ms |
2488 KB |
Output is correct |
77 |
Correct |
229 ms |
2588 KB |
Output is correct |
78 |
Correct |
250 ms |
2632 KB |
Output is correct |
79 |
Correct |
216 ms |
2432 KB |
Output is correct |
80 |
Correct |
228 ms |
2420 KB |
Output is correct |
81 |
Correct |
246 ms |
2388 KB |
Output is correct |
82 |
Correct |
235 ms |
2420 KB |
Output is correct |
83 |
Correct |
250 ms |
2460 KB |
Output is correct |
84 |
Correct |
236 ms |
2332 KB |
Output is correct |
85 |
Correct |
238 ms |
2460 KB |
Output is correct |
86 |
Correct |
257 ms |
2452 KB |
Output is correct |
87 |
Correct |
250 ms |
2360 KB |
Output is correct |
88 |
Correct |
243 ms |
2340 KB |
Output is correct |
89 |
Correct |
240 ms |
2240 KB |
Output is correct |
90 |
Correct |
250 ms |
2276 KB |
Output is correct |
91 |
Correct |
244 ms |
2316 KB |
Output is correct |
92 |
Correct |
244 ms |
2292 KB |
Output is correct |