#include <bits/stdc++.h>
using namespace std;
const int MAXN = 800;
int n, s;
vector<vector<char>> grid;
vector<vector<int>> dist;
int honey;
bool reachable(int mecho, int bee) {
return ((mecho-honey)/s < bee-honey) && (mecho != -1) && (bee != -1);
}
void bfs(queue<pair<int, int>> &q, vector<vector<int>> &d) {
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x < 1 || y < 1 || x > n || y > n)
continue;
if (d[x+1][y] == -1 && grid[x+1][y] != 'X' && reachable(d[x][y]+1, dist[x+1][y])) {
q.push({x+1, y});
d[x+1][y] = d[x][y]+1;
}
if (d[x-1][y] == -1 && grid[x-1][y] != 'X' && reachable(d[x][y]+1, dist[x-1][y])) {
q.push({x-1, y});
d[x-1][y] = d[x][y]+1;
}
if (d[x][y+1] == -1 && grid[x][y+1] != 'X' && reachable(d[x][y]+1, dist[x][y+1])) {
q.push({x, y+1});
d[x][y+1] = d[x][y]+1;
}
if (d[x][y-1] == -1 && grid[x][y-1] != 'X' && reachable(d[x][y]+1, dist[x][y-1])) {
q.push({x, y-1});
d[x][y-1] = d[x][y]+1;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> n >> s;
grid.resize(n+2, vector<char>(n+2, '.'));
pair<int, int> start, end;
queue<pair<int, int>> q;
dist.resize(n+2, vector<int>(n+2, -1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> grid[i][j];
if (grid[i][j] == 'G')
grid[i][j] = '.';
else if (grid[i][j] == 'T')
grid[i][j] = 'X';
else if (grid[i][j] == 'M')
start = {i, j};
else if (grid[i][j] == 'D')
end = {i, j};
else {
q.push({i, j});
dist[i][j] = 0;
}
}
}
// distance for bees
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (x < 1 || y < 1 || x > n || y > n)
continue;
if (dist[x+1][y] == -1 && grid[x+1][y] != 'X') {
q.push({x+1, y});
dist[x+1][y] = dist[x][y]+1;
}
if (dist[x-1][y] == -1 && grid[x-1][y] != 'X') {
q.push({x-1, y});
dist[x-1][y] = dist[x][y]+1;
}
if (dist[x][y+1] == -1 && grid[x][y+1] != 'X') {
q.push({x, y+1});
dist[x][y+1] = dist[x][y]+1;
}
if (dist[x][y-1] == -1 && grid[x][y-1] != 'X') {
q.push({x, y-1});
dist[x][y-1] = dist[x][y]+1;
}
}
int lo = 0, hi = MAXN*MAXN;
int ans = -1;
while (lo <= hi) {
int mid = (lo+hi)/2;
vector<vector<int>> d(n+2, vector<int>(n+2, -1));
d[start.first][start.second] = mid;
honey = mid;
q.push(start);
bfs(q, d);
int temp = dist[end.first][end.second];
bool poss = reachable(d[end.first-1][end.second], temp);
poss |= reachable(d[end.first+1][end.second], temp);
poss |= reachable(d[end.first][end.second-1], temp);
poss |= reachable(d[end.first][end.second+1], temp);
if (poss) {
lo = mid+1;
ans = mid;
}
else {
hi = mid-1;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Incorrect |
52 ms |
6268 KB |
Output isn't correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
13 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
452 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
5 ms |
1568 KB |
Output is correct |
34 |
Correct |
5 ms |
1584 KB |
Output is correct |
35 |
Correct |
12 ms |
1588 KB |
Output is correct |
36 |
Correct |
7 ms |
1884 KB |
Output is correct |
37 |
Correct |
7 ms |
1884 KB |
Output is correct |
38 |
Correct |
17 ms |
1792 KB |
Output is correct |
39 |
Correct |
8 ms |
2300 KB |
Output is correct |
40 |
Correct |
7 ms |
2300 KB |
Output is correct |
41 |
Correct |
21 ms |
2368 KB |
Output is correct |
42 |
Correct |
10 ms |
2768 KB |
Output is correct |
43 |
Correct |
10 ms |
2756 KB |
Output is correct |
44 |
Correct |
28 ms |
2720 KB |
Output is correct |
45 |
Correct |
12 ms |
3224 KB |
Output is correct |
46 |
Correct |
13 ms |
3272 KB |
Output is correct |
47 |
Correct |
32 ms |
3252 KB |
Output is correct |
48 |
Correct |
14 ms |
3712 KB |
Output is correct |
49 |
Correct |
14 ms |
3924 KB |
Output is correct |
50 |
Correct |
39 ms |
3708 KB |
Output is correct |
51 |
Correct |
15 ms |
4340 KB |
Output is correct |
52 |
Correct |
15 ms |
4316 KB |
Output is correct |
53 |
Correct |
41 ms |
4324 KB |
Output is correct |
54 |
Correct |
19 ms |
5176 KB |
Output is correct |
55 |
Correct |
19 ms |
4924 KB |
Output is correct |
56 |
Correct |
56 ms |
4892 KB |
Output is correct |
57 |
Correct |
22 ms |
5592 KB |
Output is correct |
58 |
Correct |
22 ms |
5596 KB |
Output is correct |
59 |
Correct |
65 ms |
5556 KB |
Output is correct |
60 |
Correct |
27 ms |
6320 KB |
Output is correct |
61 |
Correct |
26 ms |
6256 KB |
Output is correct |
62 |
Correct |
70 ms |
6236 KB |
Output is correct |
63 |
Correct |
78 ms |
6212 KB |
Output is correct |
64 |
Correct |
140 ms |
6208 KB |
Output is correct |
65 |
Correct |
103 ms |
6184 KB |
Output is correct |
66 |
Correct |
86 ms |
6232 KB |
Output is correct |
67 |
Correct |
87 ms |
6208 KB |
Output is correct |
68 |
Correct |
45 ms |
6244 KB |
Output is correct |
69 |
Correct |
45 ms |
6260 KB |
Output is correct |
70 |
Correct |
40 ms |
6224 KB |
Output is correct |
71 |
Correct |
42 ms |
6316 KB |
Output is correct |
72 |
Incorrect |
35 ms |
6252 KB |
Output isn't correct |
73 |
Incorrect |
34 ms |
6232 KB |
Output isn't correct |
74 |
Correct |
40 ms |
6316 KB |
Output is correct |
75 |
Correct |
49 ms |
6244 KB |
Output is correct |
76 |
Correct |
41 ms |
6248 KB |
Output is correct |
77 |
Correct |
43 ms |
6236 KB |
Output is correct |
78 |
Correct |
47 ms |
6232 KB |
Output is correct |
79 |
Correct |
36 ms |
6268 KB |
Output is correct |
80 |
Correct |
40 ms |
6276 KB |
Output is correct |
81 |
Correct |
46 ms |
6276 KB |
Output is correct |
82 |
Correct |
48 ms |
6308 KB |
Output is correct |
83 |
Correct |
52 ms |
6260 KB |
Output is correct |
84 |
Correct |
46 ms |
6272 KB |
Output is correct |
85 |
Correct |
52 ms |
6264 KB |
Output is correct |
86 |
Correct |
47 ms |
6244 KB |
Output is correct |
87 |
Correct |
49 ms |
6248 KB |
Output is correct |
88 |
Correct |
52 ms |
6232 KB |
Output is correct |
89 |
Correct |
51 ms |
6236 KB |
Output is correct |
90 |
Correct |
65 ms |
6232 KB |
Output is correct |
91 |
Correct |
50 ms |
6280 KB |
Output is correct |
92 |
Correct |
57 ms |
6252 KB |
Output is correct |