#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, s, beginx, beginy, endx, endy;
char arr[801][801];
queue <pair <int, int>> cur;
int distbee[801][801] = {}; bool vis[801][801];
int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0};
inline bool okbee (int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n && (arr[x][y] == 'G' || arr[x][y] == 'M' || arr[x][y] == 'H')); }
void beefs () {
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) distbee[i][j] = 1e9;
memset(vis, false, sizeof(vis)); int cnt = 0; while (!cur.empty()) {
int u = cur.size(); while (u--) {
auto k = cur.front(); cur.pop(); if (vis[k.first][k.second]) continue; vis[k.first][k.second] = 1; distbee[k.first][k.second] = cnt;
for (int i = 0; i < 4; i++) {
if (okbee(k.first + dx[i], k.second + dy[i]) && !vis[k.first + dx[i]][k.second + dy[i]]) cur.push({k.first + dx[i], k.second + dy[i]});
}
}
cnt++;
}
}
inline bool okmecho (int x, int y) { return (x >= 1 && x <= n && y >= 1 && y <= n && (arr[x][y] == 'G' || arr[x][y] == 'M' || arr[x][y] == 'D')); }
bool check (int x) {
if (x * s >= distbee[beginx][beginy]) return false;
queue <pair <int, int>> cur; bool vis[n + 1][n + 1]; memset(vis, false, sizeof(vis)); cur.push({beginx, beginy});
int cnt = x * s;
while (!cur.empty()) {
auto u = cur.size(); while (u--) {
auto k = cur.front(); cur.pop(); if (vis[k.first][k.second]) continue; vis[k.first][k.second] = 1;
if (cnt >= distbee[k.first][k.second]) continue;
if (k.first == endx && k.second == endy) return true;
for (int i = 0; i < 4; i++) {
if (okmecho(k.first + dx[i], k.second + dy[i]) && !vis[k.first + dx[i]][k.second + dy[i]]) cur.push({k.first + dx[i], k.second + dy[i]});
}
}
cnt++;
}
return false;
}
signed main () {
cin >> n >> s; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> arr[i][j];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (arr[i][j] == 'M') {
beginx = i; beginy = j;
} else if (arr[i][j] == 'D') {
endx = i; endy = j;
}
}
}
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (arr[i][j] == 'H') cur.push({i, j});
beefs();
for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) distbee[i][j] *= s;
int l = 0, r = 1e9, mid, ans = -1; while (l <= r) {
mid = (l + r) >> 1; if (check(mid)) {
l = mid + 1;
ans = mid;
} else r = mid - 1;
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
852 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
948 KB |
Output is correct |
5 |
Correct |
1 ms |
956 KB |
Output is correct |
6 |
Correct |
1 ms |
952 KB |
Output is correct |
7 |
Correct |
140 ms |
8196 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
1 ms |
980 KB |
Output is correct |
11 |
Correct |
1 ms |
952 KB |
Output is correct |
12 |
Correct |
1 ms |
1236 KB |
Output is correct |
13 |
Correct |
1 ms |
1204 KB |
Output is correct |
14 |
Correct |
2 ms |
1236 KB |
Output is correct |
15 |
Correct |
1 ms |
980 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
980 KB |
Output is correct |
20 |
Correct |
1 ms |
1108 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
1 ms |
1080 KB |
Output is correct |
23 |
Correct |
1 ms |
1108 KB |
Output is correct |
24 |
Correct |
1 ms |
1108 KB |
Output is correct |
25 |
Correct |
1 ms |
1236 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
2 ms |
1236 KB |
Output is correct |
28 |
Correct |
1 ms |
1236 KB |
Output is correct |
29 |
Correct |
1 ms |
1236 KB |
Output is correct |
30 |
Correct |
1 ms |
1236 KB |
Output is correct |
31 |
Correct |
2 ms |
1340 KB |
Output is correct |
32 |
Correct |
2 ms |
1236 KB |
Output is correct |
33 |
Correct |
10 ms |
3568 KB |
Output is correct |
34 |
Correct |
10 ms |
3540 KB |
Output is correct |
35 |
Correct |
30 ms |
3668 KB |
Output is correct |
36 |
Correct |
13 ms |
4052 KB |
Output is correct |
37 |
Correct |
13 ms |
4036 KB |
Output is correct |
38 |
Correct |
41 ms |
4180 KB |
Output is correct |
39 |
Correct |
16 ms |
4500 KB |
Output is correct |
40 |
Correct |
16 ms |
4372 KB |
Output is correct |
41 |
Correct |
51 ms |
4508 KB |
Output is correct |
42 |
Correct |
20 ms |
4932 KB |
Output is correct |
43 |
Correct |
18 ms |
4924 KB |
Output is correct |
44 |
Correct |
61 ms |
4932 KB |
Output is correct |
45 |
Correct |
22 ms |
5388 KB |
Output is correct |
46 |
Correct |
22 ms |
5384 KB |
Output is correct |
47 |
Correct |
76 ms |
5412 KB |
Output is correct |
48 |
Correct |
28 ms |
5836 KB |
Output is correct |
49 |
Correct |
27 ms |
5820 KB |
Output is correct |
50 |
Correct |
92 ms |
5888 KB |
Output is correct |
51 |
Correct |
31 ms |
6220 KB |
Output is correct |
52 |
Correct |
31 ms |
6312 KB |
Output is correct |
53 |
Correct |
105 ms |
6452 KB |
Output is correct |
54 |
Correct |
36 ms |
6820 KB |
Output is correct |
55 |
Correct |
35 ms |
6824 KB |
Output is correct |
56 |
Correct |
126 ms |
6792 KB |
Output is correct |
57 |
Correct |
43 ms |
7276 KB |
Output is correct |
58 |
Correct |
40 ms |
7244 KB |
Output is correct |
59 |
Correct |
140 ms |
7224 KB |
Output is correct |
60 |
Correct |
46 ms |
7696 KB |
Output is correct |
61 |
Correct |
47 ms |
7876 KB |
Output is correct |
62 |
Correct |
190 ms |
7840 KB |
Output is correct |
63 |
Correct |
134 ms |
7888 KB |
Output is correct |
64 |
Correct |
207 ms |
7704 KB |
Output is correct |
65 |
Correct |
211 ms |
7820 KB |
Output is correct |
66 |
Correct |
149 ms |
7820 KB |
Output is correct |
67 |
Correct |
139 ms |
7836 KB |
Output is correct |
68 |
Correct |
83 ms |
7848 KB |
Output is correct |
69 |
Correct |
76 ms |
7756 KB |
Output is correct |
70 |
Correct |
61 ms |
7756 KB |
Output is correct |
71 |
Correct |
64 ms |
7860 KB |
Output is correct |
72 |
Correct |
66 ms |
7756 KB |
Output is correct |
73 |
Correct |
99 ms |
8928 KB |
Output is correct |
74 |
Correct |
106 ms |
8844 KB |
Output is correct |
75 |
Correct |
114 ms |
8828 KB |
Output is correct |
76 |
Correct |
108 ms |
8908 KB |
Output is correct |
77 |
Correct |
134 ms |
8888 KB |
Output is correct |
78 |
Correct |
118 ms |
8620 KB |
Output is correct |
79 |
Correct |
95 ms |
8780 KB |
Output is correct |
80 |
Correct |
104 ms |
8812 KB |
Output is correct |
81 |
Correct |
117 ms |
8736 KB |
Output is correct |
82 |
Correct |
100 ms |
8812 KB |
Output is correct |
83 |
Correct |
126 ms |
8524 KB |
Output is correct |
84 |
Correct |
116 ms |
8648 KB |
Output is correct |
85 |
Correct |
118 ms |
8540 KB |
Output is correct |
86 |
Correct |
127 ms |
8620 KB |
Output is correct |
87 |
Correct |
126 ms |
8596 KB |
Output is correct |
88 |
Correct |
132 ms |
8396 KB |
Output is correct |
89 |
Correct |
127 ms |
8380 KB |
Output is correct |
90 |
Correct |
133 ms |
8348 KB |
Output is correct |
91 |
Correct |
138 ms |
8372 KB |
Output is correct |
92 |
Correct |
133 ms |
8356 KB |
Output is correct |