#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) {
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 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
7 |
Incorrect |
649 ms |
6116 KB |
Output isn't 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 |
440 KB |
Output is correct |
12 |
Incorrect |
2 ms |
716 KB |
Output isn't correct |
13 |
Correct |
3 ms |
588 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 |
2 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 |
604 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
2 ms |
716 KB |
Output is correct |
26 |
Correct |
2 ms |
716 KB |
Output is correct |
27 |
Correct |
2 ms |
716 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
3 ms |
716 KB |
Output is correct |
30 |
Correct |
3 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 |
59 ms |
2764 KB |
Output is correct |
34 |
Correct |
48 ms |
2764 KB |
Output is correct |
35 |
Correct |
84 ms |
2884 KB |
Output is correct |
36 |
Correct |
65 ms |
3152 KB |
Output is correct |
37 |
Correct |
62 ms |
3152 KB |
Output is correct |
38 |
Correct |
105 ms |
3148 KB |
Output is correct |
39 |
Correct |
82 ms |
3404 KB |
Output is correct |
40 |
Correct |
77 ms |
3500 KB |
Output is correct |
41 |
Correct |
134 ms |
3524 KB |
Output is correct |
42 |
Correct |
102 ms |
3856 KB |
Output is correct |
43 |
Correct |
95 ms |
3852 KB |
Output is correct |
44 |
Correct |
169 ms |
3788 KB |
Output is correct |
45 |
Correct |
123 ms |
4220 KB |
Output is correct |
46 |
Correct |
113 ms |
4212 KB |
Output is correct |
47 |
Correct |
201 ms |
4172 KB |
Output is correct |
48 |
Correct |
143 ms |
4560 KB |
Output is correct |
49 |
Correct |
137 ms |
4572 KB |
Output is correct |
50 |
Correct |
254 ms |
4588 KB |
Output is correct |
51 |
Correct |
179 ms |
4940 KB |
Output is correct |
52 |
Correct |
178 ms |
4940 KB |
Output is correct |
53 |
Correct |
286 ms |
4940 KB |
Output is correct |
54 |
Correct |
215 ms |
5196 KB |
Output is correct |
55 |
Correct |
182 ms |
5196 KB |
Output is correct |
56 |
Correct |
328 ms |
5276 KB |
Output is correct |
57 |
Correct |
247 ms |
5580 KB |
Output is correct |
58 |
Correct |
211 ms |
5700 KB |
Output is correct |
59 |
Correct |
420 ms |
5632 KB |
Output is correct |
60 |
Correct |
259 ms |
6084 KB |
Output is correct |
61 |
Correct |
244 ms |
6048 KB |
Output is correct |
62 |
Correct |
435 ms |
5992 KB |
Output is correct |
63 |
Correct |
530 ms |
5992 KB |
Output is correct |
64 |
Correct |
529 ms |
5964 KB |
Output is correct |
65 |
Correct |
568 ms |
6040 KB |
Output is correct |
66 |
Correct |
537 ms |
5984 KB |
Output is correct |
67 |
Correct |
542 ms |
5992 KB |
Output is correct |
68 |
Correct |
589 ms |
6032 KB |
Output is correct |
69 |
Correct |
568 ms |
6024 KB |
Output is correct |
70 |
Correct |
526 ms |
6016 KB |
Output is correct |
71 |
Correct |
562 ms |
6016 KB |
Output is correct |
72 |
Correct |
610 ms |
6032 KB |
Output is correct |
73 |
Correct |
585 ms |
6492 KB |
Output is correct |
74 |
Correct |
649 ms |
6412 KB |
Output is correct |
75 |
Correct |
659 ms |
6288 KB |
Output is correct |
76 |
Correct |
652 ms |
6284 KB |
Output is correct |
77 |
Correct |
648 ms |
6288 KB |
Output is correct |
78 |
Correct |
665 ms |
6376 KB |
Output is correct |
79 |
Correct |
693 ms |
6256 KB |
Output is correct |
80 |
Correct |
683 ms |
6340 KB |
Output is correct |
81 |
Correct |
650 ms |
6152 KB |
Output is correct |
82 |
Correct |
675 ms |
6256 KB |
Output is correct |
83 |
Correct |
659 ms |
6220 KB |
Output is correct |
84 |
Correct |
652 ms |
6328 KB |
Output is correct |
85 |
Correct |
640 ms |
6204 KB |
Output is correct |
86 |
Correct |
669 ms |
6208 KB |
Output is correct |
87 |
Correct |
653 ms |
6140 KB |
Output is correct |
88 |
Correct |
665 ms |
6160 KB |
Output is correct |
89 |
Correct |
638 ms |
6092 KB |
Output is correct |
90 |
Correct |
646 ms |
6152 KB |
Output is correct |
91 |
Correct |
653 ms |
6212 KB |
Output is correct |
92 |
Correct |
649 ms |
6248 KB |
Output is correct |