#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 805;
const int oo = 1e9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
char a[N][N];
pair <int, int> s,t;
int bee[N][N];
int dis[N][N];
int n,d;
bool ok(int i, int j) {
return a[i][j] != 'T' && min(i, j) > 0 && max(i, j) <= n;
}
bool solve(int x, queue <pair <int, int>> q) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (a[i][j] != 'H')
bee[i][j] = oo;
if (a[i][j] != 'M')
dis[i][j] = oo;
}
int fir = -1;
while (q.size()) {
int i = q.front().fi;
int j = q.front().se;
if (fir < 0)
fir = bee[i][j];
if (bee[i][j] - fir == x)
break;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int u = i + dx[dir];
int v = j + dy[dir];
if (ok(u, v) && bee[u][v] == oo) {
bee[u][v] = bee[i][j] + 1;
q.push(mp(u, v));
}
}
}
if (bee[s.fi][s.se] < oo)
return false;
queue <pair <int, int>> qq;
qq.push(s);
while (qq.size()) {
fir = -1;
while (qq.size()) {
int i = qq.front().fi;
int j = qq.front().se;
if (fir < 0)
fir = dis[i][j];
if (dis[i][j] - fir == d)
break;
qq.pop();
if (mp(i, j) == t)
return true;
if (bee[i][j] < oo)
continue;
for (int dir = 0; dir < 4; dir++) {
int u = i + dx[dir];
int v = j + dy[dir];
if (ok(u, v) && bee[u][v] == oo && dis[u][v] == oo) {
dis[u][v] = dis[i][j] + 1;
qq.push(mp(u, v));
}
}
}
fir = -1;
while (q.size()) {
int i = q.front().fi;
int j = q.front().se;
if (fir < 0)
fir = bee[i][j];
if (bee[i][j] - fir == 1)
break;
q.pop();
for (int dir = 0; dir < 4; dir++) {
int u = i + dx[dir];
int v = j + dy[dir];
if (ok(u, v) && bee[u][v] == oo && a[u][v] != 'D') {
bee[u][v] = bee[i][j] + 1;
q.push(mp(u, v));
}
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> d;
queue <pair <int, int>> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == 'H')
q.push(mp(i, j));
if (a[i][j] == 'M')
s = mp(i, j);
if (a[i][j] == 'D')
t = mp(i, j);
}
int lo = -1;
int hi = 1e9;
while (hi - lo > 1) {
int mid = (lo + hi) >> 1;
if (solve(mid, q))
lo = mid;
else
hi = mid;
}
cout << lo;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
664 ms |
6604 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Incorrect |
2 ms |
716 KB |
Output isn't correct |
13 |
Correct |
3 ms |
580 KB |
Output is correct |
14 |
Correct |
4 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
2 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
704 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
3 ms |
716 KB |
Output is correct |
28 |
Correct |
3 ms |
716 KB |
Output is correct |
29 |
Correct |
3 ms |
716 KB |
Output is correct |
30 |
Correct |
4 ms |
716 KB |
Output is correct |
31 |
Correct |
3 ms |
844 KB |
Output is correct |
32 |
Correct |
3 ms |
844 KB |
Output is correct |
33 |
Correct |
54 ms |
2896 KB |
Output is correct |
34 |
Correct |
48 ms |
2908 KB |
Output is correct |
35 |
Correct |
84 ms |
2916 KB |
Output is correct |
36 |
Correct |
76 ms |
3304 KB |
Output is correct |
37 |
Correct |
66 ms |
3316 KB |
Output is correct |
38 |
Correct |
108 ms |
3276 KB |
Output is correct |
39 |
Correct |
86 ms |
3664 KB |
Output is correct |
40 |
Correct |
98 ms |
3700 KB |
Output is correct |
41 |
Correct |
137 ms |
3660 KB |
Output is correct |
42 |
Correct |
110 ms |
4096 KB |
Output is correct |
43 |
Correct |
96 ms |
4048 KB |
Output is correct |
44 |
Correct |
175 ms |
4048 KB |
Output is correct |
45 |
Correct |
123 ms |
4504 KB |
Output is correct |
46 |
Correct |
113 ms |
4632 KB |
Output is correct |
47 |
Correct |
211 ms |
4436 KB |
Output is correct |
48 |
Correct |
149 ms |
4812 KB |
Output is correct |
49 |
Correct |
137 ms |
4812 KB |
Output is correct |
50 |
Correct |
248 ms |
4936 KB |
Output is correct |
51 |
Correct |
177 ms |
5324 KB |
Output is correct |
52 |
Correct |
160 ms |
5316 KB |
Output is correct |
53 |
Correct |
283 ms |
5332 KB |
Output is correct |
54 |
Correct |
201 ms |
5768 KB |
Output is correct |
55 |
Correct |
183 ms |
5736 KB |
Output is correct |
56 |
Correct |
335 ms |
5740 KB |
Output is correct |
57 |
Correct |
235 ms |
6212 KB |
Output is correct |
58 |
Correct |
207 ms |
6136 KB |
Output is correct |
59 |
Correct |
385 ms |
6092 KB |
Output is correct |
60 |
Correct |
302 ms |
6420 KB |
Output is correct |
61 |
Correct |
249 ms |
6488 KB |
Output is correct |
62 |
Correct |
438 ms |
6568 KB |
Output is correct |
63 |
Correct |
545 ms |
6492 KB |
Output is correct |
64 |
Correct |
529 ms |
6476 KB |
Output is correct |
65 |
Correct |
561 ms |
6488 KB |
Output is correct |
66 |
Correct |
545 ms |
6596 KB |
Output is correct |
67 |
Correct |
506 ms |
6476 KB |
Output is correct |
68 |
Correct |
631 ms |
6520 KB |
Output is correct |
69 |
Correct |
556 ms |
6520 KB |
Output is correct |
70 |
Correct |
543 ms |
6516 KB |
Output is correct |
71 |
Correct |
581 ms |
6528 KB |
Output is correct |
72 |
Correct |
657 ms |
6516 KB |
Output is correct |
73 |
Correct |
654 ms |
6792 KB |
Output is correct |
74 |
Correct |
669 ms |
6792 KB |
Output is correct |
75 |
Correct |
721 ms |
6796 KB |
Output is correct |
76 |
Correct |
829 ms |
6792 KB |
Output is correct |
77 |
Correct |
699 ms |
6800 KB |
Output is correct |
78 |
Correct |
757 ms |
6876 KB |
Output is correct |
79 |
Correct |
757 ms |
6756 KB |
Output is correct |
80 |
Correct |
683 ms |
6852 KB |
Output is correct |
81 |
Correct |
672 ms |
6764 KB |
Output is correct |
82 |
Correct |
665 ms |
6756 KB |
Output is correct |
83 |
Correct |
668 ms |
6708 KB |
Output is correct |
84 |
Correct |
661 ms |
6728 KB |
Output is correct |
85 |
Correct |
667 ms |
6708 KB |
Output is correct |
86 |
Correct |
662 ms |
6724 KB |
Output is correct |
87 |
Correct |
658 ms |
6708 KB |
Output is correct |
88 |
Correct |
668 ms |
6660 KB |
Output is correct |
89 |
Correct |
660 ms |
6604 KB |
Output is correct |
90 |
Correct |
669 ms |
6660 KB |
Output is correct |
91 |
Correct |
664 ms |
6724 KB |
Output is correct |
92 |
Correct |
725 ms |
6660 KB |
Output is correct |