#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});
}
}
vector<pii> used;
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) {
used.pb({nxt_u, nxt_v});
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});
used.pb({x, y});
}
}
}
for (auto it : used) {
q.push({it, 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 |
0 ms |
340 KB |
Output isn't correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
7 |
Correct |
183 ms |
4484 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 |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 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 |
1 ms |
340 KB |
Output isn't correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
20 |
Correct |
1 ms |
484 KB |
Output is correct |
21 |
Incorrect |
0 ms |
468 KB |
Output isn't correct |
22 |
Correct |
1 ms |
464 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 |
5 ms |
1876 KB |
Output isn't correct |
34 |
Correct |
8 ms |
1868 KB |
Output is correct |
35 |
Incorrect |
68 ms |
2640 KB |
Output isn't correct |
36 |
Incorrect |
6 ms |
2112 KB |
Output isn't correct |
37 |
Correct |
13 ms |
2132 KB |
Output is correct |
38 |
Incorrect |
77 ms |
3200 KB |
Output isn't correct |
39 |
Incorrect |
8 ms |
2348 KB |
Output isn't correct |
40 |
Correct |
12 ms |
2260 KB |
Output is correct |
41 |
Incorrect |
90 ms |
3856 KB |
Output isn't correct |
42 |
Incorrect |
9 ms |
2592 KB |
Output isn't correct |
43 |
Correct |
13 ms |
2636 KB |
Output is correct |
44 |
Incorrect |
117 ms |
4360 KB |
Output isn't correct |
45 |
Incorrect |
11 ms |
2772 KB |
Output isn't correct |
46 |
Correct |
17 ms |
2896 KB |
Output is correct |
47 |
Incorrect |
141 ms |
5244 KB |
Output isn't correct |
48 |
Incorrect |
13 ms |
3028 KB |
Output isn't correct |
49 |
Correct |
19 ms |
3152 KB |
Output is correct |
50 |
Incorrect |
185 ms |
5932 KB |
Output isn't correct |
51 |
Incorrect |
21 ms |
3372 KB |
Output isn't correct |
52 |
Correct |
33 ms |
3412 KB |
Output is correct |
53 |
Incorrect |
206 ms |
6596 KB |
Output isn't correct |
54 |
Incorrect |
24 ms |
3632 KB |
Output isn't correct |
55 |
Correct |
37 ms |
3668 KB |
Output is correct |
56 |
Incorrect |
248 ms |
7068 KB |
Output isn't correct |
57 |
Incorrect |
20 ms |
3796 KB |
Output isn't correct |
58 |
Correct |
30 ms |
3960 KB |
Output is correct |
59 |
Incorrect |
267 ms |
8160 KB |
Output isn't correct |
60 |
Incorrect |
21 ms |
4052 KB |
Output isn't correct |
61 |
Correct |
46 ms |
4216 KB |
Output is correct |
62 |
Incorrect |
350 ms |
9188 KB |
Output isn't correct |
63 |
Incorrect |
162 ms |
4160 KB |
Output isn't correct |
64 |
Correct |
352 ms |
10824 KB |
Output is correct |
65 |
Incorrect |
359 ms |
4180 KB |
Output isn't correct |
66 |
Incorrect |
254 ms |
4168 KB |
Output isn't correct |
67 |
Correct |
220 ms |
4052 KB |
Output is correct |
68 |
Incorrect |
81 ms |
4212 KB |
Output isn't correct |
69 |
Correct |
53 ms |
4600 KB |
Output is correct |
70 |
Incorrect |
43 ms |
4080 KB |
Output isn't correct |
71 |
Incorrect |
27 ms |
4076 KB |
Output isn't correct |
72 |
Correct |
28 ms |
4460 KB |
Output is correct |
73 |
Correct |
52 ms |
4332 KB |
Output is correct |
74 |
Correct |
137 ms |
6616 KB |
Output is correct |
75 |
Incorrect |
152 ms |
4436 KB |
Output isn't correct |
76 |
Incorrect |
161 ms |
4436 KB |
Output isn't correct |
77 |
Incorrect |
161 ms |
4428 KB |
Output isn't correct |
78 |
Correct |
163 ms |
4396 KB |
Output is correct |
79 |
Correct |
109 ms |
6928 KB |
Output is correct |
80 |
Correct |
128 ms |
4400 KB |
Output is correct |
81 |
Correct |
194 ms |
4512 KB |
Output is correct |
82 |
Correct |
136 ms |
4396 KB |
Output is correct |
83 |
Incorrect |
164 ms |
4360 KB |
Output isn't correct |
84 |
Correct |
175 ms |
8692 KB |
Output is correct |
85 |
Correct |
151 ms |
4356 KB |
Output is correct |
86 |
Correct |
187 ms |
4320 KB |
Output is correct |
87 |
Correct |
209 ms |
4484 KB |
Output is correct |
88 |
Incorrect |
169 ms |
4428 KB |
Output isn't correct |
89 |
Correct |
227 ms |
8748 KB |
Output is correct |
90 |
Correct |
256 ms |
4596 KB |
Output is correct |
91 |
Correct |
195 ms |
4856 KB |
Output is correct |
92 |
Correct |
214 ms |
4412 KB |
Output is correct |