#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();
//cout << "point " << p.f << ' ' << p.s << endl;
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();
#ifdef HOME
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << bee_dist[i][j] << ' ';
}
cout << endl;
}
#endif
int l = -1, r = 2 * N;
while (l < r - 1) {
int m = (l + r) >> 1;
if (can(m))
l = m;
else
r = m;
}
cout << l << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
16256 KB |
Output is correct |
2 |
Correct |
15 ms |
16256 KB |
Output is correct |
3 |
Correct |
14 ms |
16256 KB |
Output is correct |
4 |
Correct |
14 ms |
16256 KB |
Output is correct |
5 |
Correct |
12 ms |
16256 KB |
Output is correct |
6 |
Correct |
14 ms |
16256 KB |
Output is correct |
7 |
Correct |
190 ms |
17688 KB |
Output is correct |
8 |
Correct |
14 ms |
16256 KB |
Output is correct |
9 |
Correct |
13 ms |
16256 KB |
Output is correct |
10 |
Correct |
14 ms |
16256 KB |
Output is correct |
11 |
Correct |
14 ms |
16256 KB |
Output is correct |
12 |
Correct |
17 ms |
16256 KB |
Output is correct |
13 |
Correct |
12 ms |
16256 KB |
Output is correct |
14 |
Correct |
15 ms |
16256 KB |
Output is correct |
15 |
Correct |
16 ms |
16256 KB |
Output is correct |
16 |
Correct |
16 ms |
16308 KB |
Output is correct |
17 |
Correct |
18 ms |
16256 KB |
Output is correct |
18 |
Correct |
14 ms |
16256 KB |
Output is correct |
19 |
Correct |
16 ms |
16256 KB |
Output is correct |
20 |
Correct |
15 ms |
16256 KB |
Output is correct |
21 |
Correct |
17 ms |
16224 KB |
Output is correct |
22 |
Correct |
17 ms |
16256 KB |
Output is correct |
23 |
Correct |
17 ms |
16184 KB |
Output is correct |
24 |
Correct |
16 ms |
16256 KB |
Output is correct |
25 |
Correct |
17 ms |
16256 KB |
Output is correct |
26 |
Correct |
16 ms |
16256 KB |
Output is correct |
27 |
Incorrect |
19 ms |
16256 KB |
Output isn't correct |
28 |
Correct |
18 ms |
16256 KB |
Output is correct |
29 |
Incorrect |
19 ms |
16256 KB |
Output isn't correct |
30 |
Correct |
16 ms |
16256 KB |
Output is correct |
31 |
Incorrect |
20 ms |
16232 KB |
Output isn't correct |
32 |
Correct |
17 ms |
16256 KB |
Output is correct |
33 |
Incorrect |
21 ms |
16512 KB |
Output isn't correct |
34 |
Incorrect |
22 ms |
16512 KB |
Output isn't correct |
35 |
Correct |
48 ms |
16512 KB |
Output is correct |
36 |
Incorrect |
22 ms |
16512 KB |
Output isn't correct |
37 |
Incorrect |
24 ms |
16632 KB |
Output isn't correct |
38 |
Correct |
55 ms |
16640 KB |
Output is correct |
39 |
Incorrect |
22 ms |
16640 KB |
Output isn't correct |
40 |
Incorrect |
24 ms |
16640 KB |
Output isn't correct |
41 |
Correct |
72 ms |
16632 KB |
Output is correct |
42 |
Incorrect |
24 ms |
16768 KB |
Output isn't correct |
43 |
Incorrect |
26 ms |
16768 KB |
Output isn't correct |
44 |
Correct |
87 ms |
16876 KB |
Output is correct |
45 |
Incorrect |
23 ms |
16896 KB |
Output isn't correct |
46 |
Incorrect |
24 ms |
16896 KB |
Output isn't correct |
47 |
Correct |
105 ms |
16896 KB |
Output is correct |
48 |
Incorrect |
27 ms |
17024 KB |
Output isn't correct |
49 |
Incorrect |
26 ms |
17024 KB |
Output isn't correct |
50 |
Correct |
138 ms |
17144 KB |
Output is correct |
51 |
Incorrect |
27 ms |
17152 KB |
Output isn't correct |
52 |
Incorrect |
27 ms |
17152 KB |
Output isn't correct |
53 |
Correct |
149 ms |
17152 KB |
Output is correct |
54 |
Incorrect |
29 ms |
17280 KB |
Output isn't correct |
55 |
Incorrect |
32 ms |
17280 KB |
Output isn't correct |
56 |
Correct |
181 ms |
17272 KB |
Output is correct |
57 |
Incorrect |
27 ms |
17408 KB |
Output isn't correct |
58 |
Incorrect |
30 ms |
17408 KB |
Output isn't correct |
59 |
Correct |
191 ms |
17400 KB |
Output is correct |
60 |
Incorrect |
33 ms |
17528 KB |
Output isn't correct |
61 |
Incorrect |
31 ms |
17584 KB |
Output isn't correct |
62 |
Correct |
230 ms |
17528 KB |
Output is correct |
63 |
Correct |
149 ms |
17528 KB |
Output is correct |
64 |
Correct |
236 ms |
17600 KB |
Output is correct |
65 |
Correct |
223 ms |
17528 KB |
Output is correct |
66 |
Correct |
182 ms |
17656 KB |
Output is correct |
67 |
Correct |
148 ms |
17476 KB |
Output is correct |
68 |
Correct |
70 ms |
17656 KB |
Output is correct |
69 |
Correct |
67 ms |
17528 KB |
Output is correct |
70 |
Correct |
68 ms |
17784 KB |
Output is correct |
71 |
Correct |
56 ms |
17528 KB |
Output is correct |
72 |
Correct |
43 ms |
17656 KB |
Output is correct |
73 |
Correct |
70 ms |
17740 KB |
Output is correct |
74 |
Correct |
108 ms |
17612 KB |
Output is correct |
75 |
Correct |
132 ms |
17612 KB |
Output is correct |
76 |
Correct |
115 ms |
17736 KB |
Output is correct |
77 |
Correct |
126 ms |
17612 KB |
Output is correct |
78 |
Correct |
132 ms |
17672 KB |
Output is correct |
79 |
Correct |
101 ms |
17804 KB |
Output is correct |
80 |
Correct |
117 ms |
17644 KB |
Output is correct |
81 |
Correct |
132 ms |
17668 KB |
Output is correct |
82 |
Correct |
119 ms |
17672 KB |
Output is correct |
83 |
Correct |
163 ms |
17620 KB |
Output is correct |
84 |
Correct |
146 ms |
17748 KB |
Output is correct |
85 |
Correct |
146 ms |
17748 KB |
Output is correct |
86 |
Correct |
168 ms |
17620 KB |
Output is correct |
87 |
Correct |
164 ms |
17748 KB |
Output is correct |
88 |
Correct |
171 ms |
17716 KB |
Output is correct |
89 |
Correct |
175 ms |
17720 KB |
Output is correct |
90 |
Correct |
172 ms |
17716 KB |
Output is correct |
91 |
Correct |
174 ms |
17716 KB |
Output is correct |
92 |
Correct |
170 ms |
17716 KB |
Output is correct |