#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 * S});
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();
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 + 1) {
ok[x][y] = true;
q.push({{x, y}, time + 1});
}
}
}
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;
}
}
}
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++) {
dist[i][j]--;
dist[i][j] *= S;
}
}
dist[t.fi][t.se] = 1e9;
int low = 0, high = dist[s.fi][s.se] - 1;
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 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
90 ms |
4272 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
0 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
0 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
596 KB |
Output is correct |
26 |
Correct |
1 ms |
596 KB |
Output is correct |
27 |
Correct |
1 ms |
596 KB |
Output is correct |
28 |
Correct |
1 ms |
596 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
596 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
3 ms |
1748 KB |
Output is correct |
34 |
Correct |
5 ms |
1748 KB |
Output is correct |
35 |
Correct |
19 ms |
1884 KB |
Output is correct |
36 |
Correct |
4 ms |
2004 KB |
Output is correct |
37 |
Correct |
3 ms |
2004 KB |
Output is correct |
38 |
Correct |
21 ms |
2116 KB |
Output is correct |
39 |
Correct |
4 ms |
2260 KB |
Output is correct |
40 |
Correct |
4 ms |
2260 KB |
Output is correct |
41 |
Correct |
35 ms |
2336 KB |
Output is correct |
42 |
Correct |
7 ms |
2516 KB |
Output is correct |
43 |
Correct |
6 ms |
2608 KB |
Output is correct |
44 |
Correct |
44 ms |
2588 KB |
Output is correct |
45 |
Correct |
8 ms |
2848 KB |
Output is correct |
46 |
Incorrect |
19 ms |
2844 KB |
Output isn't correct |
47 |
Correct |
41 ms |
2772 KB |
Output is correct |
48 |
Correct |
8 ms |
3112 KB |
Output is correct |
49 |
Incorrect |
11 ms |
3116 KB |
Output isn't correct |
50 |
Correct |
52 ms |
3120 KB |
Output is correct |
51 |
Correct |
8 ms |
3372 KB |
Output is correct |
52 |
Incorrect |
20 ms |
3372 KB |
Output isn't correct |
53 |
Correct |
63 ms |
3380 KB |
Output is correct |
54 |
Correct |
13 ms |
3540 KB |
Output is correct |
55 |
Incorrect |
33 ms |
3540 KB |
Output isn't correct |
56 |
Correct |
94 ms |
3628 KB |
Output is correct |
57 |
Correct |
14 ms |
3904 KB |
Output is correct |
58 |
Incorrect |
27 ms |
3880 KB |
Output isn't correct |
59 |
Correct |
79 ms |
3908 KB |
Output is correct |
60 |
Correct |
13 ms |
4132 KB |
Output is correct |
61 |
Incorrect |
31 ms |
4152 KB |
Output isn't correct |
62 |
Correct |
123 ms |
4160 KB |
Output is correct |
63 |
Correct |
93 ms |
4160 KB |
Output is correct |
64 |
Correct |
146 ms |
4164 KB |
Output is correct |
65 |
Correct |
149 ms |
4180 KB |
Output is correct |
66 |
Correct |
89 ms |
4152 KB |
Output is correct |
67 |
Correct |
106 ms |
4156 KB |
Output is correct |
68 |
Correct |
36 ms |
4180 KB |
Output is correct |
69 |
Correct |
31 ms |
4180 KB |
Output is correct |
70 |
Correct |
31 ms |
4172 KB |
Output is correct |
71 |
Correct |
30 ms |
4176 KB |
Output is correct |
72 |
Incorrect |
25 ms |
4084 KB |
Output isn't correct |
73 |
Incorrect |
31 ms |
4456 KB |
Output isn't correct |
74 |
Correct |
42 ms |
4332 KB |
Output is correct |
75 |
Correct |
51 ms |
4436 KB |
Output is correct |
76 |
Correct |
47 ms |
4436 KB |
Output is correct |
77 |
Correct |
57 ms |
4328 KB |
Output is correct |
78 |
Correct |
53 ms |
4312 KB |
Output is correct |
79 |
Correct |
52 ms |
4368 KB |
Output is correct |
80 |
Correct |
45 ms |
4404 KB |
Output is correct |
81 |
Correct |
70 ms |
4404 KB |
Output is correct |
82 |
Correct |
45 ms |
4404 KB |
Output is correct |
83 |
Correct |
55 ms |
4360 KB |
Output is correct |
84 |
Correct |
57 ms |
4488 KB |
Output is correct |
85 |
Correct |
49 ms |
4308 KB |
Output is correct |
86 |
Correct |
57 ms |
4360 KB |
Output is correct |
87 |
Correct |
67 ms |
4352 KB |
Output is correct |
88 |
Correct |
56 ms |
4308 KB |
Output is correct |
89 |
Correct |
58 ms |
4316 KB |
Output is correct |
90 |
Correct |
60 ms |
4312 KB |
Output is correct |
91 |
Correct |
64 ms |
4248 KB |
Output is correct |
92 |
Correct |
54 ms |
4312 KB |
Output is correct |