#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 800 + 5;
char matrix[MAXN][MAXN];
int distBee[MAXN][MAXN];
int distBear[MAXN][MAXN];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define ONLINE_JUDGE
void solve() {
memset(distBee, -1, sizeof(distBee));
int n, s;
cin >> n >> s;
pair <int, int> initial;
pair <int, int> home;
queue <array <int, 3>> q;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
cin >> matrix[i][j];
if(matrix[i][j] == 'H') {
q.push({0, i, j});
} else if(matrix[i][j] == 'M') {
initial = {i, j};
} else if(matrix[i][j] == 'D') {
home = {i, j};
}
}
}
auto in = [&](int x, int y) -> bool {
return 1 <= min(x, y) && max(x, y) <= n;
};
while(!q.empty()) {
auto [d, x, y] = q.front();
q.pop();
if(distBee[x][y] != -1) {
continue;
}
distBee[x][y] = d;
for(int dir = 0; dir < 4; dir++) {
int _x = x + dx[dir], _y = y + dy[dir];
if(in(_x, _y) && matrix[_x][_y] == 'G') {
q.push({d +1, _x, _y});
}
}
}
for(int ans = 0; ; ans++) {
memset(distBear, -1, sizeof(distBear));
q.push({s * ans, initial.first, initial.second});
while(!q.empty()) {
auto [d, x, y] = q.front();
q.pop();
if(distBear[x][y] != -1 || (distBee[x][y] != -1 && d / s >= distBee[x][y])) {
continue;
}
//cerr << d << " :: " << x << " " << y << " | " << distBee[x][y] << "\n";
distBear[x][y] = d;
for(int dir = 0; dir < 4; dir++) {
int _x = x + dx[dir], _y = y + dy[dir];
if(in(_x, _y) && (matrix[_x][_y] == 'G' || matrix[_x][_y] == 'D')) {
q.push({d +1, _x, _y});
}
}
}
if(distBear[home.first][home.second] == -1) {
cout << ans -1;
return;
}
//cerr << "\n";
}
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5360 KB |
Output is correct |
2 |
Correct |
1 ms |
5468 KB |
Output is correct |
3 |
Correct |
1 ms |
5468 KB |
Output is correct |
4 |
Correct |
1 ms |
5468 KB |
Output is correct |
5 |
Correct |
2 ms |
5464 KB |
Output is correct |
6 |
Correct |
1 ms |
5468 KB |
Output is correct |
7 |
Correct |
307 ms |
7352 KB |
Output is correct |
8 |
Correct |
1 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5468 KB |
Output is correct |
10 |
Correct |
1 ms |
5468 KB |
Output is correct |
11 |
Correct |
1 ms |
5468 KB |
Output is correct |
12 |
Execution timed out |
1075 ms |
5340 KB |
Time limit exceeded |
13 |
Incorrect |
1 ms |
5464 KB |
Output isn't correct |
14 |
Correct |
1 ms |
5468 KB |
Output is correct |
15 |
Correct |
8 ms |
5352 KB |
Output is correct |
16 |
Correct |
5 ms |
5468 KB |
Output is correct |
17 |
Correct |
15 ms |
5464 KB |
Output is correct |
18 |
Correct |
7 ms |
5468 KB |
Output is correct |
19 |
Correct |
19 ms |
5468 KB |
Output is correct |
20 |
Correct |
9 ms |
5468 KB |
Output is correct |
21 |
Correct |
35 ms |
5468 KB |
Output is correct |
22 |
Correct |
18 ms |
5468 KB |
Output is correct |
23 |
Correct |
54 ms |
5464 KB |
Output is correct |
24 |
Correct |
30 ms |
5468 KB |
Output is correct |
25 |
Correct |
85 ms |
5468 KB |
Output is correct |
26 |
Correct |
50 ms |
5468 KB |
Output is correct |
27 |
Correct |
108 ms |
5568 KB |
Output is correct |
28 |
Correct |
59 ms |
5464 KB |
Output is correct |
29 |
Correct |
129 ms |
5572 KB |
Output is correct |
30 |
Correct |
71 ms |
5572 KB |
Output is correct |
31 |
Correct |
151 ms |
5464 KB |
Output is correct |
32 |
Correct |
99 ms |
5584 KB |
Output is correct |
33 |
Execution timed out |
1027 ms |
5720 KB |
Time limit exceeded |
34 |
Execution timed out |
1022 ms |
5720 KB |
Time limit exceeded |
35 |
Correct |
24 ms |
5724 KB |
Output is correct |
36 |
Execution timed out |
1065 ms |
5980 KB |
Time limit exceeded |
37 |
Execution timed out |
1062 ms |
5980 KB |
Time limit exceeded |
38 |
Correct |
32 ms |
6028 KB |
Output is correct |
39 |
Execution timed out |
1039 ms |
5980 KB |
Time limit exceeded |
40 |
Execution timed out |
1014 ms |
5976 KB |
Time limit exceeded |
41 |
Correct |
37 ms |
5980 KB |
Output is correct |
42 |
Execution timed out |
1055 ms |
6232 KB |
Time limit exceeded |
43 |
Execution timed out |
1062 ms |
5980 KB |
Time limit exceeded |
44 |
Correct |
46 ms |
5980 KB |
Output is correct |
45 |
Execution timed out |
1044 ms |
6236 KB |
Time limit exceeded |
46 |
Execution timed out |
1052 ms |
6236 KB |
Time limit exceeded |
47 |
Correct |
55 ms |
6236 KB |
Output is correct |
48 |
Execution timed out |
1042 ms |
6232 KB |
Time limit exceeded |
49 |
Execution timed out |
1069 ms |
6236 KB |
Time limit exceeded |
50 |
Correct |
67 ms |
6480 KB |
Output is correct |
51 |
Execution timed out |
1052 ms |
6276 KB |
Time limit exceeded |
52 |
Execution timed out |
1016 ms |
6232 KB |
Time limit exceeded |
53 |
Correct |
66 ms |
6488 KB |
Output is correct |
54 |
Execution timed out |
1037 ms |
6488 KB |
Time limit exceeded |
55 |
Execution timed out |
1055 ms |
6492 KB |
Time limit exceeded |
56 |
Correct |
77 ms |
6488 KB |
Output is correct |
57 |
Execution timed out |
1020 ms |
6492 KB |
Time limit exceeded |
58 |
Execution timed out |
1029 ms |
6488 KB |
Time limit exceeded |
59 |
Correct |
108 ms |
6520 KB |
Output is correct |
60 |
Execution timed out |
1014 ms |
6744 KB |
Time limit exceeded |
61 |
Execution timed out |
1004 ms |
6748 KB |
Time limit exceeded |
62 |
Correct |
105 ms |
6736 KB |
Output is correct |
63 |
Correct |
46 ms |
6636 KB |
Output is correct |
64 |
Execution timed out |
1047 ms |
6740 KB |
Time limit exceeded |
65 |
Execution timed out |
1006 ms |
6740 KB |
Time limit exceeded |
66 |
Execution timed out |
1041 ms |
6740 KB |
Time limit exceeded |
67 |
Correct |
33 ms |
6740 KB |
Output is correct |
68 |
Correct |
34 ms |
6724 KB |
Output is correct |
69 |
Correct |
151 ms |
6860 KB |
Output is correct |
70 |
Correct |
73 ms |
6868 KB |
Output is correct |
71 |
Correct |
50 ms |
6864 KB |
Output is correct |
72 |
Execution timed out |
1028 ms |
6700 KB |
Time limit exceeded |
73 |
Incorrect |
868 ms |
8364 KB |
Output isn't correct |
74 |
Execution timed out |
1035 ms |
8372 KB |
Time limit exceeded |
75 |
Correct |
983 ms |
8624 KB |
Output is correct |
76 |
Correct |
539 ms |
8480 KB |
Output is correct |
77 |
Execution timed out |
1047 ms |
8228 KB |
Time limit exceeded |
78 |
Correct |
38 ms |
8020 KB |
Output is correct |
79 |
Execution timed out |
1035 ms |
8340 KB |
Time limit exceeded |
80 |
Correct |
637 ms |
8256 KB |
Output is correct |
81 |
Correct |
373 ms |
8248 KB |
Output is correct |
82 |
Correct |
946 ms |
8276 KB |
Output is correct |
83 |
Correct |
54 ms |
7728 KB |
Output is correct |
84 |
Execution timed out |
1090 ms |
7760 KB |
Time limit exceeded |
85 |
Correct |
533 ms |
7764 KB |
Output is correct |
86 |
Correct |
297 ms |
7996 KB |
Output is correct |
87 |
Correct |
766 ms |
7764 KB |
Output is correct |
88 |
Correct |
55 ms |
7476 KB |
Output is correct |
89 |
Correct |
721 ms |
7636 KB |
Output is correct |
90 |
Correct |
320 ms |
7648 KB |
Output is correct |
91 |
Correct |
403 ms |
7636 KB |
Output is correct |
92 |
Correct |
181 ms |
7512 KB |
Output is correct |