#include <bits/stdc++.h>
using namespace std;
constexpr int NMAX = 805;
typedef pair <int, int> PII;
int N, S;
char init[NMAX][NMAX];
int viz[NMAX][NMAX];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
void Read () {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> S;
for (int i = 1; i <= N; ++ i )
for (int j = 1; j <= N; ++ j )
cin >> init[i][j];
}
bool Interior (int x, int y) {
return 0 < x && x < N+1 && 0 < y && y < N+1;
}
void Expand (queue <PII> &Bees) {
queue <PII> next;
while (!Bees.empty()) {
PII P = Bees.front();
Bees.pop();
for (int i = 0; i < 4; ++ i ) {
PII urm = {P.first + dx[i], P.second + dy[i]};
if (Interior(urm.first, urm.second) && init[urm.first][urm.second] == 'G' && viz[urm.first][urm.second] != -1) {
next.push(urm);
viz[urm.first][urm.second] = -1;
}
}
}
Bees = next;
}
bool Check (int wait) {
queue <PII> Ends, CanExp;
PII finish;
for (int i = 1; i <= N; ++ i ) {
for (int j = 1; j <= N; ++ j ) {
if (init[i][j] == 'T') viz[i][j] = -1;
if (init[i][j] == 'G') viz[i][j] = 0;
if (init[i][j] == 'M') {
Ends.push({i, j});
viz[i][j] = 1;
}
if (init[i][j] == 'D') {
finish = {i, j};
viz[i][j] = 0;
}
if (init[i][j] == 'H') {
CanExp.push({i, j});
viz[i][j] = -1;
}
}
}
for (int i = 1; i <= wait; ++ i )
Expand(CanExp);
bool finished = 0;
while (!finished && !Ends.empty()) {
for (int pas = 1; pas <= S; ++ pas ) {
queue <PII> nextEnds;
while (!Ends.empty()) {
PII P = Ends.front();
Ends.pop();
if (viz[P.first][P.second] != 1) continue;
for (int i = 0; i < 4; ++ i ) {
PII urm = {P.first + dx[i], P.second + dy[i]};
if (Interior(urm.first, urm.second) && viz[urm.first][urm.second] == 0) {
nextEnds.push(urm);
viz[urm.first][urm.second] = 1;
}
}
}
Ends = nextEnds;
}
if (viz[finish.first][finish.second]) finished = 1;
Expand(CanExp);
}
return finished;
}
void Solve () {
int st = 0, dr = N * N;
int ans = -1;
while (st <= dr) {
int mij = (st + dr) / 2;
if (Check(mij)) {
ans = mij;
st = mij + 1;
}
else dr = mij - 1;
}
cout << ans << '\n';
}
int main () {
Read();
Solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
393 ms |
4308 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
460 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
2 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
3 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
580 KB |
Output is correct |
27 |
Correct |
3 ms |
588 KB |
Output is correct |
28 |
Correct |
2 ms |
576 KB |
Output is correct |
29 |
Correct |
4 ms |
588 KB |
Output is correct |
30 |
Correct |
5 ms |
588 KB |
Output is correct |
31 |
Correct |
4 ms |
588 KB |
Output is correct |
32 |
Correct |
3 ms |
588 KB |
Output is correct |
33 |
Correct |
92 ms |
1740 KB |
Output is correct |
34 |
Correct |
66 ms |
1812 KB |
Output is correct |
35 |
Correct |
46 ms |
1740 KB |
Output is correct |
36 |
Correct |
121 ms |
2056 KB |
Output is correct |
37 |
Correct |
83 ms |
1996 KB |
Output is correct |
38 |
Correct |
57 ms |
2052 KB |
Output is correct |
39 |
Correct |
165 ms |
2300 KB |
Output is correct |
40 |
Correct |
96 ms |
2252 KB |
Output is correct |
41 |
Correct |
71 ms |
2288 KB |
Output is correct |
42 |
Correct |
181 ms |
2520 KB |
Output is correct |
43 |
Correct |
120 ms |
2508 KB |
Output is correct |
44 |
Correct |
94 ms |
2536 KB |
Output is correct |
45 |
Correct |
239 ms |
2788 KB |
Output is correct |
46 |
Correct |
145 ms |
2776 KB |
Output is correct |
47 |
Correct |
102 ms |
2764 KB |
Output is correct |
48 |
Correct |
278 ms |
3028 KB |
Output is correct |
49 |
Correct |
180 ms |
3024 KB |
Output is correct |
50 |
Correct |
124 ms |
3044 KB |
Output is correct |
51 |
Correct |
352 ms |
3304 KB |
Output is correct |
52 |
Correct |
211 ms |
3248 KB |
Output is correct |
53 |
Correct |
149 ms |
3292 KB |
Output is correct |
54 |
Correct |
438 ms |
3548 KB |
Output is correct |
55 |
Correct |
242 ms |
3532 KB |
Output is correct |
56 |
Correct |
179 ms |
3560 KB |
Output is correct |
57 |
Correct |
496 ms |
3816 KB |
Output is correct |
58 |
Correct |
297 ms |
3788 KB |
Output is correct |
59 |
Correct |
239 ms |
3828 KB |
Output is correct |
60 |
Correct |
533 ms |
4084 KB |
Output is correct |
61 |
Correct |
333 ms |
4088 KB |
Output is correct |
62 |
Correct |
255 ms |
4120 KB |
Output is correct |
63 |
Correct |
381 ms |
4096 KB |
Output is correct |
64 |
Correct |
367 ms |
4092 KB |
Output is correct |
65 |
Correct |
355 ms |
4092 KB |
Output is correct |
66 |
Correct |
383 ms |
4088 KB |
Output is correct |
67 |
Correct |
358 ms |
4088 KB |
Output is correct |
68 |
Correct |
393 ms |
4164 KB |
Output is correct |
69 |
Correct |
417 ms |
4160 KB |
Output is correct |
70 |
Correct |
488 ms |
4172 KB |
Output is correct |
71 |
Correct |
439 ms |
4148 KB |
Output is correct |
72 |
Execution timed out |
1095 ms |
4044 KB |
Time limit exceeded |
73 |
Incorrect |
366 ms |
4628 KB |
Output isn't correct |
74 |
Correct |
357 ms |
4916 KB |
Output is correct |
75 |
Correct |
415 ms |
4628 KB |
Output is correct |
76 |
Correct |
366 ms |
4660 KB |
Output is correct |
77 |
Correct |
427 ms |
4648 KB |
Output is correct |
78 |
Correct |
368 ms |
4580 KB |
Output is correct |
79 |
Correct |
367 ms |
4584 KB |
Output is correct |
80 |
Correct |
361 ms |
4604 KB |
Output is correct |
81 |
Correct |
350 ms |
4680 KB |
Output is correct |
82 |
Correct |
412 ms |
4584 KB |
Output is correct |
83 |
Correct |
390 ms |
4492 KB |
Output is correct |
84 |
Correct |
355 ms |
4508 KB |
Output is correct |
85 |
Correct |
422 ms |
4496 KB |
Output is correct |
86 |
Correct |
342 ms |
4528 KB |
Output is correct |
87 |
Correct |
369 ms |
4508 KB |
Output is correct |
88 |
Correct |
368 ms |
4408 KB |
Output is correct |
89 |
Correct |
385 ms |
4424 KB |
Output is correct |
90 |
Correct |
401 ms |
4428 KB |
Output is correct |
91 |
Correct |
348 ms |
4436 KB |
Output is correct |
92 |
Correct |
375 ms |
4428 KB |
Output is correct |