#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1007, INF = 1e9 + 7;
int n, S;
string a[N];
int bee_dist[N][N];
bool can_bee(int i, int j) {
return 0 <= i && i < n && 0 <= j && j < n && (a[i][j] == 'G' || a[i][j] == 'M' || a[i][j] == 'H');
}
vector <ii> v = { {0, -1}, {0, 1}, {-1, 0}, {1, 0} };
void bfs1() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
bee_dist[i][j] = INF;
}
}
queue <ii> q;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'H') {
bee_dist[i][j] = 0;
q.push(mp(i, j));
}
}
}
while (q.size()) {
auto p = q.front(); q.pop();
for (auto e : v) {
int x = p.f + e.f, y = p.s + e.s;
if (can_bee(x, y) && bee_dist[p.f][p.s] + 1 < bee_dist[x][y]) {
bee_dist[x][y] = bee_dist[p.f][p.s] + 1;
q.push(mp(x, y));
}
}
}
}
bool can_bear(int i, int j, int t, int m) {
t = t / S;
return 0 <= i && i < n && 0 <= j && j < n && (a[i][j] == 'G' || a[i][j] == 'M' || a[i][j] == 'D') && bee_dist[i][j] > t + m;
}
int sx, sy;
int dist[N][N];
bool can(int m) {
if (bee_dist[sx][sy] <= m)
return 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
dist[i][j] = INF;
}
}
queue <ii> q;
dist[sx][sy] = 0;
q.push(mp(sx, sy));
while (q.size()) {
auto p = q.front(); q.pop();
if (a[p.f][p.s] == 'D')
return 1;
for (auto e : v) {
int x = p.f + e.f, y = p.s + e.s;
if (can_bear(x, y, dist[p.f][p.s] + 1, m) && dist[p.f][p.s] + 1 < dist[x][y]) {
dist[x][y] = dist[p.f][p.s] + 1;
q.push(mp(x, y));
}
}
}
return 0;
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
cin >> n >> S;
for (int i = 0; i < n; ++i)
cin >> a[i];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (a[i][j] == 'M') {
sx = i;
sy = j;
}
}
}
bfs1();
int l = -1, r = INF;
while (l < r - 1) {
int m = (l + r) >> 1;
if (can(m))
l = m;
else
r = m;
}
cout << l << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
16256 KB |
Output is correct |
2 |
Correct |
15 ms |
16256 KB |
Output is correct |
3 |
Correct |
13 ms |
16256 KB |
Output is correct |
4 |
Correct |
16 ms |
16256 KB |
Output is correct |
5 |
Correct |
13 ms |
16256 KB |
Output is correct |
6 |
Correct |
16 ms |
16256 KB |
Output is correct |
7 |
Correct |
187 ms |
17056 KB |
Output is correct |
8 |
Correct |
14 ms |
16256 KB |
Output is correct |
9 |
Correct |
16 ms |
16256 KB |
Output is correct |
10 |
Correct |
14 ms |
16256 KB |
Output is correct |
11 |
Correct |
13 ms |
16256 KB |
Output is correct |
12 |
Correct |
17 ms |
16256 KB |
Output is correct |
13 |
Correct |
13 ms |
16256 KB |
Output is correct |
14 |
Correct |
17 ms |
16256 KB |
Output is correct |
15 |
Correct |
18 ms |
16256 KB |
Output is correct |
16 |
Correct |
16 ms |
16256 KB |
Output is correct |
17 |
Correct |
20 ms |
16256 KB |
Output is correct |
18 |
Correct |
15 ms |
16256 KB |
Output is correct |
19 |
Correct |
17 ms |
16256 KB |
Output is correct |
20 |
Correct |
16 ms |
16256 KB |
Output is correct |
21 |
Correct |
18 ms |
16256 KB |
Output is correct |
22 |
Correct |
14 ms |
16256 KB |
Output is correct |
23 |
Correct |
19 ms |
16256 KB |
Output is correct |
24 |
Correct |
17 ms |
16256 KB |
Output is correct |
25 |
Correct |
21 ms |
16256 KB |
Output is correct |
26 |
Correct |
19 ms |
16256 KB |
Output is correct |
27 |
Correct |
16 ms |
16256 KB |
Output is correct |
28 |
Correct |
16 ms |
16256 KB |
Output is correct |
29 |
Correct |
19 ms |
16256 KB |
Output is correct |
30 |
Correct |
19 ms |
16256 KB |
Output is correct |
31 |
Correct |
20 ms |
16256 KB |
Output is correct |
32 |
Correct |
18 ms |
16256 KB |
Output is correct |
33 |
Correct |
23 ms |
16384 KB |
Output is correct |
34 |
Correct |
20 ms |
16384 KB |
Output is correct |
35 |
Correct |
48 ms |
16384 KB |
Output is correct |
36 |
Correct |
22 ms |
16384 KB |
Output is correct |
37 |
Correct |
19 ms |
16384 KB |
Output is correct |
38 |
Correct |
66 ms |
16384 KB |
Output is correct |
39 |
Correct |
24 ms |
16512 KB |
Output is correct |
40 |
Correct |
22 ms |
16384 KB |
Output is correct |
41 |
Correct |
77 ms |
16512 KB |
Output is correct |
42 |
Correct |
23 ms |
16544 KB |
Output is correct |
43 |
Correct |
20 ms |
16512 KB |
Output is correct |
44 |
Correct |
97 ms |
16632 KB |
Output is correct |
45 |
Correct |
27 ms |
16512 KB |
Output is correct |
46 |
Correct |
24 ms |
16512 KB |
Output is correct |
47 |
Correct |
107 ms |
16512 KB |
Output is correct |
48 |
Correct |
31 ms |
16640 KB |
Output is correct |
49 |
Correct |
32 ms |
16760 KB |
Output is correct |
50 |
Correct |
130 ms |
16640 KB |
Output is correct |
51 |
Correct |
29 ms |
16640 KB |
Output is correct |
52 |
Correct |
28 ms |
16640 KB |
Output is correct |
53 |
Correct |
161 ms |
16760 KB |
Output is correct |
54 |
Correct |
30 ms |
16768 KB |
Output is correct |
55 |
Correct |
30 ms |
16768 KB |
Output is correct |
56 |
Correct |
167 ms |
16768 KB |
Output is correct |
57 |
Correct |
32 ms |
16768 KB |
Output is correct |
58 |
Correct |
26 ms |
16896 KB |
Output is correct |
59 |
Correct |
203 ms |
16768 KB |
Output is correct |
60 |
Correct |
32 ms |
16896 KB |
Output is correct |
61 |
Correct |
35 ms |
16896 KB |
Output is correct |
62 |
Correct |
225 ms |
16896 KB |
Output is correct |
63 |
Correct |
148 ms |
17012 KB |
Output is correct |
64 |
Correct |
243 ms |
16960 KB |
Output is correct |
65 |
Correct |
239 ms |
17016 KB |
Output is correct |
66 |
Correct |
163 ms |
16892 KB |
Output is correct |
67 |
Correct |
158 ms |
16888 KB |
Output is correct |
68 |
Correct |
71 ms |
17008 KB |
Output is correct |
69 |
Correct |
71 ms |
16888 KB |
Output is correct |
70 |
Correct |
49 ms |
16896 KB |
Output is correct |
71 |
Correct |
55 ms |
16888 KB |
Output is correct |
72 |
Correct |
48 ms |
16892 KB |
Output is correct |
73 |
Correct |
76 ms |
17204 KB |
Output is correct |
74 |
Correct |
113 ms |
16972 KB |
Output is correct |
75 |
Correct |
136 ms |
16972 KB |
Output is correct |
76 |
Correct |
109 ms |
16968 KB |
Output is correct |
77 |
Correct |
119 ms |
16972 KB |
Output is correct |
78 |
Correct |
138 ms |
17032 KB |
Output is correct |
79 |
Correct |
92 ms |
17040 KB |
Output is correct |
80 |
Correct |
112 ms |
17028 KB |
Output is correct |
81 |
Correct |
127 ms |
17028 KB |
Output is correct |
82 |
Correct |
101 ms |
17032 KB |
Output is correct |
83 |
Correct |
184 ms |
16980 KB |
Output is correct |
84 |
Correct |
140 ms |
17232 KB |
Output is correct |
85 |
Correct |
150 ms |
16980 KB |
Output is correct |
86 |
Correct |
159 ms |
16988 KB |
Output is correct |
87 |
Correct |
178 ms |
17108 KB |
Output is correct |
88 |
Correct |
181 ms |
17076 KB |
Output is correct |
89 |
Correct |
167 ms |
17076 KB |
Output is correct |
90 |
Correct |
174 ms |
16948 KB |
Output is correct |
91 |
Correct |
168 ms |
17076 KB |
Output is correct |
92 |
Correct |
165 ms |
17076 KB |
Output is correct |