#include <bits/stdc++.h>
using namespace std;
template<class A, class B> bool maximize(A& x, B y) {if (x < y) return x = y, true; else return false;}
template<class A, class B> bool minimize(A& x, B y) {if (x > y) return x = y, true; else return false;}
#define all(a) a.begin(), a.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;
const int MAX_N = 800 + 5;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, S;
string a[MAX_N];
pii s, t;
int dist[MAX_N][MAX_N];
bool ok[MAX_N][MAX_N];
bool inside(int x, int y) {
return x > 0 && x <= n && y > 0 && y <= n;
}
bool check(int mid) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
ok[i][j] = false;
}
}
queue<pair<pii, int>> q;
q.push({s, mid + 1});
ok[s.fi][s.se] = true;
while (!q.empty()) {
int u = q.front().fi.fi;
int v = q.front().fi.se;
int time = q.front().se;
q.pop();
queue<pii> nxt_q;
for (int k = 0; k < 4; k++) {
int x = u + dx[k];
int y = v + dy[k];
if (inside(x, y) && (a[x][y] == 'G' || a[x][y] == 'D') && !ok[x][y] && dist[x][y] > time) {
ok[x][y] = true;
nxt_q.push({x, y});
q.push({{x, y}, time + 1});
}
}
while (!nxt_q.empty()) {
int nxt_u = nxt_q.front().fi;
int nxt_v = nxt_q.front().se;
nxt_q.pop();
if (abs(nxt_u - u) + abs(nxt_v - v) == S) {
continue;
}
for (int k = 0; k < 4; k++) {
int x = nxt_u + dx[k];
int y = nxt_v + dy[k];
if (inside(x, y) && (a[x][y] == 'G' || a[x][y] == 'D') && !ok[x][y] && dist[x][y] > time) {
ok[x][y] = true;
nxt_q.push({x, y});
q.push({{x, y}, time + 1});
}
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << ok[i][j] << " ";
// }
// cout << '\n';
// }
return ok[t.fi][t.se];
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
queue<pii> q;
cin >> n >> S;
for (int i = 1; i <= n; i++) {
cin >> a[i];
a[i] = ' ' + a[i];
for (int j = 1; j <= n; j++) {
if (a[i][j] == 'M') {
s = {i, j};
}
if (a[i][j] == 'D') {
t = {i, j};
}
if (a[i][j] == 'H') {
q.push({i, j});
dist[i][j] = 1;
}
}
}
dist[t.fi][t.se] = 1e9;
while (!q.empty()) {
int u = q.front().fi;
int v = q.front().se;
q.pop();
for (int k = 0; k < 4; k++) {
int x = u + dx[k];
int y = v + dy[k];
if (inside(x, y) && (a[x][y] == 'G' || a[x][y] == 'M') && !dist[x][y]) {
dist[x][y] = dist[u][v] + 1;
q.push({x, y});
}
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= n; j++) {
// cout << dist[i][j] << " ";
// }
// cout << '\n';
// }
// cout << check(2);
// return 0;
int low = 1, high = dist[s.fi][s.se] - 2;
int ans = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (check(mid)) {
ans = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
cout << ans;
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
7 |
Correct |
159 ms |
4388 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Incorrect |
6 ms |
1876 KB |
Output isn't correct |
34 |
Correct |
10 ms |
1880 KB |
Output is correct |
35 |
Incorrect |
56 ms |
2284 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
2056 KB |
Output isn't correct |
37 |
Correct |
10 ms |
2004 KB |
Output is correct |
38 |
Incorrect |
65 ms |
2604 KB |
Output isn't correct |
39 |
Incorrect |
7 ms |
2260 KB |
Output isn't correct |
40 |
Correct |
15 ms |
2344 KB |
Output is correct |
41 |
Incorrect |
101 ms |
2984 KB |
Output isn't correct |
42 |
Incorrect |
8 ms |
2516 KB |
Output isn't correct |
43 |
Correct |
12 ms |
2612 KB |
Output is correct |
44 |
Incorrect |
106 ms |
3392 KB |
Output isn't correct |
45 |
Incorrect |
11 ms |
2748 KB |
Output isn't correct |
46 |
Correct |
23 ms |
2752 KB |
Output is correct |
47 |
Incorrect |
148 ms |
3796 KB |
Output isn't correct |
48 |
Incorrect |
11 ms |
3028 KB |
Output isn't correct |
49 |
Correct |
24 ms |
3028 KB |
Output is correct |
50 |
Incorrect |
163 ms |
4244 KB |
Output isn't correct |
51 |
Incorrect |
18 ms |
3368 KB |
Output isn't correct |
52 |
Correct |
31 ms |
3400 KB |
Output is correct |
53 |
Incorrect |
168 ms |
4624 KB |
Output isn't correct |
54 |
Incorrect |
15 ms |
3540 KB |
Output isn't correct |
55 |
Correct |
30 ms |
3656 KB |
Output is correct |
56 |
Incorrect |
220 ms |
5192 KB |
Output isn't correct |
57 |
Incorrect |
24 ms |
3800 KB |
Output isn't correct |
58 |
Correct |
39 ms |
3916 KB |
Output is correct |
59 |
Incorrect |
226 ms |
5724 KB |
Output isn't correct |
60 |
Incorrect |
26 ms |
4148 KB |
Output isn't correct |
61 |
Correct |
36 ms |
4108 KB |
Output is correct |
62 |
Incorrect |
300 ms |
6208 KB |
Output isn't correct |
63 |
Incorrect |
176 ms |
4160 KB |
Output isn't correct |
64 |
Correct |
340 ms |
8032 KB |
Output is correct |
65 |
Incorrect |
312 ms |
4168 KB |
Output isn't correct |
66 |
Incorrect |
226 ms |
4156 KB |
Output isn't correct |
67 |
Correct |
189 ms |
4164 KB |
Output is correct |
68 |
Incorrect |
66 ms |
4192 KB |
Output isn't correct |
69 |
Correct |
51 ms |
4456 KB |
Output is correct |
70 |
Incorrect |
53 ms |
4080 KB |
Output isn't correct |
71 |
Incorrect |
26 ms |
4080 KB |
Output isn't correct |
72 |
Correct |
34 ms |
4332 KB |
Output is correct |
73 |
Correct |
37 ms |
4428 KB |
Output is correct |
74 |
Correct |
119 ms |
5348 KB |
Output is correct |
75 |
Incorrect |
166 ms |
4436 KB |
Output isn't correct |
76 |
Incorrect |
107 ms |
4432 KB |
Output isn't correct |
77 |
Incorrect |
122 ms |
4332 KB |
Output isn't correct |
78 |
Correct |
160 ms |
4404 KB |
Output is correct |
79 |
Correct |
119 ms |
5852 KB |
Output is correct |
80 |
Correct |
113 ms |
4400 KB |
Output is correct |
81 |
Correct |
189 ms |
4308 KB |
Output is correct |
82 |
Correct |
125 ms |
4384 KB |
Output is correct |
83 |
Incorrect |
141 ms |
4292 KB |
Output isn't correct |
84 |
Correct |
140 ms |
5944 KB |
Output is correct |
85 |
Correct |
172 ms |
4356 KB |
Output is correct |
86 |
Correct |
182 ms |
4356 KB |
Output is correct |
87 |
Correct |
131 ms |
4428 KB |
Output is correct |
88 |
Incorrect |
141 ms |
4308 KB |
Output isn't correct |
89 |
Correct |
173 ms |
6052 KB |
Output is correct |
90 |
Correct |
192 ms |
4440 KB |
Output is correct |
91 |
Correct |
197 ms |
4684 KB |
Output is correct |
92 |
Correct |
199 ms |
4320 KB |
Output is correct |