#include <bits/stdc++.h>
using namespace std;
using ii = pair<int,int>;
using ll = long long;
const int dx[] {0, -1, 0, 1};
const int dy[] {-1, 0, 1, 0};
constexpr int MAXS = 5+800;
constexpr int INF = 1e8;
char c[MAXS][MAXS];
int N,S, tb[MAXS][MAXS];
bool ok(int x, int y) {
return x >= 0 && y >= 0 && x < N && y < N;
}
void bfs_bees() {
queue<ii> q;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (c[i][j] == 'H') {
q.push({i,j});
tb[i][j] = 0;
} else {
tb[i][j] = 10*INF;
}
}
}
while (!q.empty()) {
auto u = q.front(); q.pop();
for (int k = 0; k < 4; ++k) {
int newx = u.first+dx[k], newy = u.second+dy[k];
if (ok(newx, newy) && 1+tb[u.first][u.second] < tb[newx][newy] && c[newx][newy] == 'G') {
q.push({newx, newy});
tb[newx][newy] = 1+tb[u.first][u.second];
}
}
}
}
ii dist[MAXS][MAXS];
bool can(const ii& source, const ii& target, int t) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
dist[i][j] = {10*INF, 0};
}
}
queue<array<int,2>> q; q.push({source.first, source.second});
dist[source.first][source.second] = {t, S};
while (!q.empty()) {
auto u = q.front(); q.pop();
if (u[0] == target.first && u[1] == target.second) return true;
for (int k = 0; k < 4; ++k) {
int newx = u[0]+dx[k], newy = u[1]+dy[k];
ii newcost = dist[u[0]][u[1]];
newcost.second--;
if (newcost.second <= 0) {
newcost.first++;
newcost.second = S;
}
if (ok(newx, newy) && (c[newx][newy] == 'G' || c[newx][newy] == 'D') && (newcost.first < tb[newx][newy]) &&
(newcost.first < dist[newx][newy].first || (newcost.first == dist[newx][newy].first && newcost.second > dist[newx][newy].second))) {
q.push({newx, newy});
dist[newx][newy] = newcost;
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> N >> S;
ii start, target;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> c[i][j];
if (c[i][j] == 'M') {
start = {i,j};
} else if (c[i][j] == 'D') {
target = {i,j};
}
}
}
bfs_bees();
int low = 0, high = INF, best = -1;
while (low <= high) {
int mid = (low+high) >> 1;
if (can(start, target, mid)) {
low = 1+mid;
best = mid;
} else {
high = mid-1;
}
}
cout << best << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
156 ms |
8696 KB |
Output is correct |
8 |
Correct |
0 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Incorrect |
1 ms |
896 KB |
Output isn't correct |
13 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
14 |
Correct |
1 ms |
896 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
0 ms |
512 KB |
Output is correct |
19 |
Correct |
0 ms |
640 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
640 KB |
Output is correct |
23 |
Correct |
1 ms |
768 KB |
Output is correct |
24 |
Correct |
1 ms |
768 KB |
Output is correct |
25 |
Correct |
1 ms |
800 KB |
Output is correct |
26 |
Correct |
1 ms |
896 KB |
Output is correct |
27 |
Correct |
1 ms |
896 KB |
Output is correct |
28 |
Correct |
1 ms |
896 KB |
Output is correct |
29 |
Correct |
1 ms |
896 KB |
Output is correct |
30 |
Correct |
1 ms |
896 KB |
Output is correct |
31 |
Correct |
1 ms |
1024 KB |
Output is correct |
32 |
Correct |
1 ms |
1024 KB |
Output is correct |
33 |
Correct |
8 ms |
3968 KB |
Output is correct |
34 |
Correct |
8 ms |
3992 KB |
Output is correct |
35 |
Correct |
24 ms |
3968 KB |
Output is correct |
36 |
Correct |
9 ms |
4480 KB |
Output is correct |
37 |
Correct |
10 ms |
4480 KB |
Output is correct |
38 |
Correct |
34 ms |
4480 KB |
Output is correct |
39 |
Correct |
11 ms |
4992 KB |
Output is correct |
40 |
Correct |
11 ms |
4992 KB |
Output is correct |
41 |
Correct |
42 ms |
4992 KB |
Output is correct |
42 |
Correct |
14 ms |
5504 KB |
Output is correct |
43 |
Correct |
14 ms |
5504 KB |
Output is correct |
44 |
Correct |
60 ms |
5504 KB |
Output is correct |
45 |
Correct |
17 ms |
6016 KB |
Output is correct |
46 |
Correct |
17 ms |
6016 KB |
Output is correct |
47 |
Correct |
69 ms |
6016 KB |
Output is correct |
48 |
Correct |
20 ms |
6528 KB |
Output is correct |
49 |
Correct |
20 ms |
6528 KB |
Output is correct |
50 |
Correct |
85 ms |
6528 KB |
Output is correct |
51 |
Correct |
23 ms |
7040 KB |
Output is correct |
52 |
Correct |
22 ms |
7040 KB |
Output is correct |
53 |
Correct |
103 ms |
7040 KB |
Output is correct |
54 |
Correct |
26 ms |
7552 KB |
Output is correct |
55 |
Correct |
26 ms |
7544 KB |
Output is correct |
56 |
Correct |
119 ms |
7544 KB |
Output is correct |
57 |
Correct |
39 ms |
8184 KB |
Output is correct |
58 |
Correct |
37 ms |
8056 KB |
Output is correct |
59 |
Correct |
166 ms |
8060 KB |
Output is correct |
60 |
Correct |
37 ms |
8568 KB |
Output is correct |
61 |
Correct |
38 ms |
8568 KB |
Output is correct |
62 |
Correct |
219 ms |
8568 KB |
Output is correct |
63 |
Correct |
127 ms |
8576 KB |
Output is correct |
64 |
Correct |
169 ms |
8568 KB |
Output is correct |
65 |
Correct |
196 ms |
8572 KB |
Output is correct |
66 |
Correct |
153 ms |
8576 KB |
Output is correct |
67 |
Correct |
153 ms |
8584 KB |
Output is correct |
68 |
Correct |
67 ms |
8568 KB |
Output is correct |
69 |
Correct |
65 ms |
8568 KB |
Output is correct |
70 |
Correct |
56 ms |
8568 KB |
Output is correct |
71 |
Correct |
55 ms |
8568 KB |
Output is correct |
72 |
Incorrect |
51 ms |
8576 KB |
Output isn't correct |
73 |
Incorrect |
95 ms |
8680 KB |
Output isn't correct |
74 |
Correct |
80 ms |
8672 KB |
Output is correct |
75 |
Correct |
85 ms |
8672 KB |
Output is correct |
76 |
Correct |
89 ms |
8672 KB |
Output is correct |
77 |
Correct |
92 ms |
8672 KB |
Output is correct |
78 |
Correct |
102 ms |
8828 KB |
Output is correct |
79 |
Correct |
83 ms |
8700 KB |
Output is correct |
80 |
Correct |
84 ms |
8700 KB |
Output is correct |
81 |
Correct |
92 ms |
8700 KB |
Output is correct |
82 |
Correct |
78 ms |
8704 KB |
Output is correct |
83 |
Correct |
108 ms |
8608 KB |
Output is correct |
84 |
Correct |
101 ms |
8608 KB |
Output is correct |
85 |
Correct |
116 ms |
8776 KB |
Output is correct |
86 |
Correct |
126 ms |
8712 KB |
Output is correct |
87 |
Correct |
113 ms |
8608 KB |
Output is correct |
88 |
Correct |
117 ms |
8660 KB |
Output is correct |
89 |
Correct |
121 ms |
8668 KB |
Output is correct |
90 |
Correct |
128 ms |
8788 KB |
Output is correct |
91 |
Correct |
133 ms |
8788 KB |
Output is correct |
92 |
Correct |
135 ms |
8660 KB |
Output is correct |