#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' || init[urm.first][urm.second] == 'M') && 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;
}
int aux[NMAX][NMAX];
int FindRight () {
queue <PII> Q;
for (int i = 1; i <= N; ++ i )
for (int j = 1; j <= N; ++ j ) {
aux[i][j] = 0;
if (init[i][j] == 'H') {
Q.push({i, j});
aux[i][j] = 1;
}
}
while (!Q.empty()) {
PII x = Q.front();
Q.pop();
for (int i = 0; i < 4; ++ i ) {
PII urm = {x.first + dx[i], x.second + dy[i]};
if (Interior(urm.first, urm.second) && aux[urm.first][urm.second] == 0){
aux[urm.first][urm.second] = aux[x.first][x.second]+1;
Q.push(urm);
}
}
}
for (int i = 1; i <= N; ++ i )
for (int j = 1; j <= N; ++ j )
if (init[i][j] == 'M') return (aux[i][j] == 0 ? N*N : aux[i][j]);
}
void Solve () {
int st = 0, dr = FindRight();
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;
}
Compilation message
mecho.cpp: In function 'int FindRight()':
mecho.cpp:112:17: warning: control reaches end of non-void function [-Wreturn-type]
112 | queue <PII> Q;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
163 ms |
6196 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
13 |
Correct |
1 ms |
588 KB |
Output is correct |
14 |
Correct |
1 ms |
716 KB |
Output is correct |
15 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
460 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
588 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
844 KB |
Output isn't correct |
33 |
Incorrect |
7 ms |
2764 KB |
Output isn't correct |
34 |
Incorrect |
8 ms |
2764 KB |
Output isn't correct |
35 |
Correct |
19 ms |
2816 KB |
Output is correct |
36 |
Incorrect |
11 ms |
3064 KB |
Output isn't correct |
37 |
Incorrect |
10 ms |
3148 KB |
Output isn't correct |
38 |
Correct |
26 ms |
3172 KB |
Output is correct |
39 |
Incorrect |
10 ms |
3404 KB |
Output isn't correct |
40 |
Incorrect |
10 ms |
3404 KB |
Output isn't correct |
41 |
Correct |
31 ms |
3404 KB |
Output is correct |
42 |
Incorrect |
11 ms |
3832 KB |
Output isn't correct |
43 |
Incorrect |
13 ms |
3848 KB |
Output isn't correct |
44 |
Correct |
41 ms |
3788 KB |
Output is correct |
45 |
Incorrect |
19 ms |
4172 KB |
Output isn't correct |
46 |
Incorrect |
15 ms |
4236 KB |
Output isn't correct |
47 |
Correct |
44 ms |
4168 KB |
Output is correct |
48 |
Incorrect |
16 ms |
4556 KB |
Output isn't correct |
49 |
Incorrect |
18 ms |
4556 KB |
Output isn't correct |
50 |
Correct |
54 ms |
4492 KB |
Output is correct |
51 |
Incorrect |
20 ms |
4940 KB |
Output isn't correct |
52 |
Incorrect |
21 ms |
4936 KB |
Output isn't correct |
53 |
Correct |
63 ms |
4936 KB |
Output is correct |
54 |
Incorrect |
21 ms |
5264 KB |
Output isn't correct |
55 |
Incorrect |
30 ms |
5264 KB |
Output isn't correct |
56 |
Correct |
72 ms |
5316 KB |
Output is correct |
57 |
Incorrect |
25 ms |
5580 KB |
Output isn't correct |
58 |
Incorrect |
26 ms |
5588 KB |
Output isn't correct |
59 |
Correct |
82 ms |
5636 KB |
Output is correct |
60 |
Incorrect |
27 ms |
5964 KB |
Output isn't correct |
61 |
Incorrect |
28 ms |
5988 KB |
Output isn't correct |
62 |
Correct |
104 ms |
5908 KB |
Output is correct |
63 |
Correct |
138 ms |
5964 KB |
Output is correct |
64 |
Incorrect |
168 ms |
6060 KB |
Output isn't correct |
65 |
Correct |
159 ms |
5908 KB |
Output is correct |
66 |
Incorrect |
166 ms |
5988 KB |
Output isn't correct |
67 |
Correct |
158 ms |
5992 KB |
Output is correct |
68 |
Correct |
116 ms |
6136 KB |
Output is correct |
69 |
Correct |
73 ms |
6072 KB |
Output is correct |
70 |
Correct |
124 ms |
6136 KB |
Output is correct |
71 |
Correct |
112 ms |
6148 KB |
Output is correct |
72 |
Incorrect |
112 ms |
6140 KB |
Output isn't correct |
73 |
Correct |
41 ms |
6596 KB |
Output is correct |
74 |
Correct |
160 ms |
6536 KB |
Output is correct |
75 |
Correct |
143 ms |
6532 KB |
Output is correct |
76 |
Correct |
173 ms |
6540 KB |
Output is correct |
77 |
Correct |
161 ms |
6696 KB |
Output is correct |
78 |
Correct |
142 ms |
6480 KB |
Output is correct |
79 |
Correct |
145 ms |
6492 KB |
Output is correct |
80 |
Correct |
166 ms |
6480 KB |
Output is correct |
81 |
Correct |
164 ms |
6676 KB |
Output is correct |
82 |
Correct |
162 ms |
6484 KB |
Output is correct |
83 |
Correct |
193 ms |
6408 KB |
Output is correct |
84 |
Correct |
157 ms |
6416 KB |
Output is correct |
85 |
Correct |
156 ms |
6520 KB |
Output is correct |
86 |
Correct |
193 ms |
6400 KB |
Output is correct |
87 |
Correct |
168 ms |
6392 KB |
Output is correct |
88 |
Correct |
171 ms |
6224 KB |
Output is correct |
89 |
Correct |
138 ms |
6516 KB |
Output is correct |
90 |
Correct |
155 ms |
6288 KB |
Output is correct |
91 |
Correct |
170 ms |
6300 KB |
Output is correct |
92 |
Correct |
153 ms |
6312 KB |
Output is correct |