#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 8e2 + 20;
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
bool grass[maxn][maxn];
int dist_h[maxn][maxn];
int dist_m[maxn][maxn];
int n, k, sx, sy, ex, ey;
bool check() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != sx || j != sy) {
dist_m[i][j] = -1;
}
}
}
deque<pair<int, int>> q;
q.emplace_back(sx, sy);
while (!q.empty()) {
int cx = q.front().first;
int cy = q.front().second;
q.pop_front();
for (int d = 0; d < 4; d++) {
int nx = cx + dx[d];
int ny = cy + dy[d];
if (nx == ex && ny == ey) {
return true;
}
if (dist_m[nx][ny] == -1 && dist_m[cx][cy] + 1 < dist_h[nx][ny] * k) {
dist_m[nx][ny] = dist_m[cx][cy] + 1;
q.emplace_back(nx, ny);
}
}
}
return false;
}
void just_do_it() {
cin >> n >> k;
deque<pair<int, int>> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char c;
cin >> c;
if (c == 'G') {
grass[i][j] = true;
}
if (c == 'M') {
grass[i][j] = true;
sx = i;
sy = j;
}
if (c == 'D') {
ex = i;
ey = j;
}
if (c == 'H') {
dist_h[i][j] = 0;
q.emplace_back(i, j);
}
else {
dist_h[i][j] = -1;
}
}
}
while (!q.empty()) {
int cx = q.front().first;
int cy = q.front().second;
q.pop_front();
for (int d = 0; d < 4; d++) {
int nx = cx + dx[d];
int ny = cy + dy[d];
if (grass[nx][ny] && dist_h[nx][ny] == -1) {
dist_h[nx][ny] = dist_h[cx][cy] + 1;
q.emplace_back(nx, ny);
}
}
}
int res = -1;
int lt = 0;
int rt = dist_h[sx][sy] - 1;
while (lt <= rt) {
int mt = (lt + rt) / 2;
dist_m[sx][sy] = mt * k;
if (check()) {
res = mt;
lt = mt + 1;
}
else {
rt = mt - 1;
}
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
82 ms |
6844 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
716 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
464 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
596 KB |
Output is correct |
24 |
Correct |
1 ms |
596 KB |
Output is correct |
25 |
Correct |
1 ms |
716 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
28 |
Correct |
1 ms |
720 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
852 KB |
Output is correct |
33 |
Correct |
9 ms |
2900 KB |
Output is correct |
34 |
Correct |
6 ms |
2980 KB |
Output is correct |
35 |
Correct |
16 ms |
2900 KB |
Output is correct |
36 |
Correct |
7 ms |
3284 KB |
Output is correct |
37 |
Correct |
9 ms |
3284 KB |
Output is correct |
38 |
Correct |
21 ms |
3340 KB |
Output is correct |
39 |
Correct |
9 ms |
3676 KB |
Output is correct |
40 |
Correct |
10 ms |
3668 KB |
Output is correct |
41 |
Correct |
26 ms |
3664 KB |
Output is correct |
42 |
Correct |
12 ms |
4180 KB |
Output is correct |
43 |
Correct |
11 ms |
4180 KB |
Output is correct |
44 |
Correct |
32 ms |
4180 KB |
Output is correct |
45 |
Correct |
12 ms |
4556 KB |
Output is correct |
46 |
Correct |
12 ms |
4564 KB |
Output is correct |
47 |
Correct |
39 ms |
4484 KB |
Output is correct |
48 |
Correct |
16 ms |
4936 KB |
Output is correct |
49 |
Correct |
15 ms |
5000 KB |
Output is correct |
50 |
Correct |
52 ms |
5032 KB |
Output is correct |
51 |
Correct |
18 ms |
5372 KB |
Output is correct |
52 |
Correct |
18 ms |
5372 KB |
Output is correct |
53 |
Correct |
58 ms |
5336 KB |
Output is correct |
54 |
Correct |
19 ms |
5844 KB |
Output is correct |
55 |
Correct |
22 ms |
5812 KB |
Output is correct |
56 |
Correct |
73 ms |
5860 KB |
Output is correct |
57 |
Correct |
24 ms |
6252 KB |
Output is correct |
58 |
Correct |
23 ms |
6228 KB |
Output is correct |
59 |
Correct |
77 ms |
6300 KB |
Output is correct |
60 |
Correct |
26 ms |
6656 KB |
Output is correct |
61 |
Correct |
26 ms |
6596 KB |
Output is correct |
62 |
Correct |
101 ms |
6740 KB |
Output is correct |
63 |
Correct |
107 ms |
6728 KB |
Output is correct |
64 |
Correct |
168 ms |
6724 KB |
Output is correct |
65 |
Correct |
153 ms |
6736 KB |
Output is correct |
66 |
Correct |
117 ms |
6608 KB |
Output is correct |
67 |
Correct |
129 ms |
6604 KB |
Output is correct |
68 |
Correct |
51 ms |
6756 KB |
Output is correct |
69 |
Correct |
46 ms |
6744 KB |
Output is correct |
70 |
Correct |
48 ms |
6744 KB |
Output is correct |
71 |
Correct |
42 ms |
6688 KB |
Output is correct |
72 |
Correct |
39 ms |
6644 KB |
Output is correct |
73 |
Correct |
35 ms |
6920 KB |
Output is correct |
74 |
Correct |
54 ms |
6988 KB |
Output is correct |
75 |
Correct |
50 ms |
7000 KB |
Output is correct |
76 |
Correct |
55 ms |
7008 KB |
Output is correct |
77 |
Correct |
63 ms |
6944 KB |
Output is correct |
78 |
Correct |
59 ms |
6972 KB |
Output is correct |
79 |
Correct |
48 ms |
6972 KB |
Output is correct |
80 |
Correct |
59 ms |
6868 KB |
Output is correct |
81 |
Correct |
56 ms |
6940 KB |
Output is correct |
82 |
Correct |
52 ms |
6868 KB |
Output is correct |
83 |
Correct |
64 ms |
6952 KB |
Output is correct |
84 |
Correct |
55 ms |
6900 KB |
Output is correct |
85 |
Correct |
58 ms |
6808 KB |
Output is correct |
86 |
Correct |
61 ms |
6864 KB |
Output is correct |
87 |
Correct |
64 ms |
6924 KB |
Output is correct |
88 |
Correct |
70 ms |
6884 KB |
Output is correct |
89 |
Correct |
62 ms |
6884 KB |
Output is correct |
90 |
Correct |
77 ms |
6880 KB |
Output is correct |
91 |
Correct |
67 ms |
6872 KB |
Output is correct |
92 |
Correct |
84 ms |
6872 KB |
Output is correct |