#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair <int, int>
#define ill pair <ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define file "test"
using namespace std;
const ll N = 8e2 + 2;
const ll MOD = 1e9;
const ll INF = 1e18;
const ll base = 311;
const int BLOCK_SIZE = 2000;
int n, s;
ii start, target, d[N][N];
char a[N][N];
int mtime[N][N];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
void addstep(ii &a) {
a.se += 1;
if (a.se == s) a.fi ++, a.se = 0;
}
bool cmp(ii a, ii b) {
if (a.fi != b.fi) return a.fi < b.fi;
return a.se < b.se;
}
bool cmp2(ii a, int b) {
if (a.fi != b) return a.fi < b;
return a.se < s;
}
bool check(int x) {
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++) d[i][j] = {2e9, 0};
queue <ii> h;
h.push(start);
d[start.fi][start.se] = {x + 1, 0};
while (!h.empty()) {
ii e = h.front(); h.pop();
int x = e.fi, y = e.se;
ii res = d[x][y];
addstep(res);
for (int j = 0; j <= 3; j ++) {
int u = x + dx[j];
int v = y + dy[j];
if (a[u][v] == 'G' && cmp2(res, mtime[u][v]) && !cmp(d[u][v], res)) {
h.push({u, v});
d[u][v] = res;
}
}
}
return d[target.fi][target.se].fi != 2e9;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> s;
queue <ii> bee;
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++) {
cin >> a[i][j];
mtime[i][j] = 2e9;
if (a[i][j] == 'H')
bee.push({i, j}), mtime[i][j] = 0;
if (a[i][j] == 'M')
a[i][j] = 'G', start = {i, j};
if (a[i][j] == 'D')
target = {i, j};
}
while (!bee.empty()) {
ii e = bee.front(); bee.pop();
int x = e.fi, y = e.se;
int res = mtime[x][y] + 1;
for (int j = 0; j <= 3; j ++) {
int u = x + dx[j];
int v = y + dy[j];
if (a[u][v] == 'G' && mtime[u][v] > res) {
bee.push({u, v});
mtime[u][v] = res;
}
}
}
a[target.fi][target.se] = 'G';
int l = 0, r = 1e9;
while (l < r) {
int mid = l + (r - l + 1) / 2;
if (check(mid)) l = mid;
else r = mid - 1;
}
cout << (check(l) ? l : -1) << '\n';
}
/*
/\_/\ zzZ
(= -_-)
/ >u >u
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 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 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
8 |
Correct |
1 ms |
460 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 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
13 |
Runtime error |
290 ms |
65536 KB |
Execution killed with signal 9 |
14 |
Runtime error |
204 ms |
65536 KB |
Execution killed with signal 9 |
15 |
Correct |
1 ms |
456 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 |
468 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 |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
596 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
852 KB |
Output is correct |
26 |
Correct |
1 ms |
840 KB |
Output is correct |
27 |
Correct |
1 ms |
852 KB |
Output is correct |
28 |
Correct |
1 ms |
852 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 |
976 KB |
Output is correct |
32 |
Correct |
1 ms |
980 KB |
Output is correct |
33 |
Correct |
7 ms |
4028 KB |
Output is correct |
34 |
Correct |
7 ms |
3924 KB |
Output is correct |
35 |
Runtime error |
191 ms |
65536 KB |
Execution killed with signal 9 |
36 |
Correct |
8 ms |
4436 KB |
Output is correct |
37 |
Correct |
8 ms |
4436 KB |
Output is correct |
38 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
39 |
Correct |
11 ms |
4948 KB |
Output is correct |
40 |
Correct |
10 ms |
5060 KB |
Output is correct |
41 |
Runtime error |
190 ms |
65536 KB |
Execution killed with signal 9 |
42 |
Correct |
12 ms |
5460 KB |
Output is correct |
43 |
Correct |
11 ms |
5504 KB |
Output is correct |
44 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
45 |
Correct |
13 ms |
6100 KB |
Output is correct |
46 |
Correct |
15 ms |
6040 KB |
Output is correct |
47 |
Runtime error |
193 ms |
65536 KB |
Execution killed with signal 9 |
48 |
Correct |
17 ms |
6612 KB |
Output is correct |
49 |
Correct |
20 ms |
6692 KB |
Output is correct |
50 |
Runtime error |
190 ms |
65536 KB |
Execution killed with signal 9 |
51 |
Correct |
19 ms |
7148 KB |
Output is correct |
52 |
Correct |
19 ms |
7104 KB |
Output is correct |
53 |
Runtime error |
195 ms |
65536 KB |
Execution killed with signal 9 |
54 |
Correct |
23 ms |
7636 KB |
Output is correct |
55 |
Correct |
23 ms |
7660 KB |
Output is correct |
56 |
Runtime error |
196 ms |
65536 KB |
Execution killed with signal 9 |
57 |
Correct |
24 ms |
8140 KB |
Output is correct |
58 |
Correct |
23 ms |
8140 KB |
Output is correct |
59 |
Runtime error |
196 ms |
65536 KB |
Execution killed with signal 9 |
60 |
Correct |
30 ms |
8928 KB |
Output is correct |
61 |
Correct |
27 ms |
8736 KB |
Output is correct |
62 |
Runtime error |
197 ms |
65536 KB |
Execution killed with signal 9 |
63 |
Correct |
94 ms |
8620 KB |
Output is correct |
64 |
Correct |
147 ms |
8736 KB |
Output is correct |
65 |
Correct |
156 ms |
8664 KB |
Output is correct |
66 |
Correct |
112 ms |
8628 KB |
Output is correct |
67 |
Correct |
100 ms |
8680 KB |
Output is correct |
68 |
Correct |
48 ms |
8696 KB |
Output is correct |
69 |
Correct |
50 ms |
8740 KB |
Output is correct |
70 |
Correct |
46 ms |
8696 KB |
Output is correct |
71 |
Correct |
40 ms |
8692 KB |
Output is correct |
72 |
Incorrect |
36 ms |
8752 KB |
Output isn't correct |
73 |
Runtime error |
192 ms |
65536 KB |
Execution killed with signal 9 |
74 |
Runtime error |
193 ms |
65536 KB |
Execution killed with signal 9 |
75 |
Runtime error |
195 ms |
65536 KB |
Execution killed with signal 9 |
76 |
Runtime error |
192 ms |
65536 KB |
Execution killed with signal 9 |
77 |
Runtime error |
189 ms |
65536 KB |
Execution killed with signal 9 |
78 |
Runtime error |
190 ms |
65536 KB |
Execution killed with signal 9 |
79 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
80 |
Runtime error |
187 ms |
65536 KB |
Execution killed with signal 9 |
81 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
82 |
Runtime error |
193 ms |
65536 KB |
Execution killed with signal 9 |
83 |
Runtime error |
198 ms |
65536 KB |
Execution killed with signal 9 |
84 |
Runtime error |
198 ms |
65536 KB |
Execution killed with signal 9 |
85 |
Runtime error |
197 ms |
65536 KB |
Execution killed with signal 9 |
86 |
Runtime error |
199 ms |
65536 KB |
Execution killed with signal 9 |
87 |
Runtime error |
196 ms |
65536 KB |
Execution killed with signal 9 |
88 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
89 |
Runtime error |
188 ms |
65536 KB |
Execution killed with signal 9 |
90 |
Runtime error |
200 ms |
65536 KB |
Execution killed with signal 9 |
91 |
Runtime error |
191 ms |
65536 KB |
Execution killed with signal 9 |
92 |
Runtime error |
192 ms |
65536 KB |
Execution killed with signal 9 |