#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/s) <= bee-honey) || (bee == -1)) && (mecho != -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);
if (reachable(d[end.first][end.second], dist[end.first][end.second])) {
lo = mid+1;
ans = mid;
}
else {
hi = mid;
}
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
452 KB |
Output isn't correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Correct |
57 ms |
6836 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
0 ms |
456 KB |
Output is correct |
10 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
11 |
Correct |
0 ms |
344 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
17 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
18 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
19 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
22 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
23 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
24 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
27 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
28 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
29 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
30 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
31 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
32 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
33 |
Incorrect |
5 ms |
1748 KB |
Output isn't correct |
34 |
Incorrect |
5 ms |
1700 KB |
Output isn't correct |
35 |
Incorrect |
14 ms |
1748 KB |
Output isn't correct |
36 |
Incorrect |
7 ms |
1948 KB |
Output isn't correct |
37 |
Incorrect |
6 ms |
2084 KB |
Output isn't correct |
38 |
Incorrect |
18 ms |
2112 KB |
Output isn't correct |
39 |
Incorrect |
8 ms |
2556 KB |
Output isn't correct |
40 |
Incorrect |
8 ms |
2508 KB |
Output isn't correct |
41 |
Incorrect |
22 ms |
2508 KB |
Output isn't correct |
42 |
Incorrect |
10 ms |
3264 KB |
Output isn't correct |
43 |
Incorrect |
10 ms |
2960 KB |
Output isn't correct |
44 |
Incorrect |
24 ms |
3020 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
3544 KB |
Output isn't correct |
46 |
Incorrect |
12 ms |
3492 KB |
Output isn't correct |
47 |
Incorrect |
31 ms |
3524 KB |
Output isn't correct |
48 |
Incorrect |
13 ms |
4132 KB |
Output isn't correct |
49 |
Incorrect |
13 ms |
4140 KB |
Output isn't correct |
50 |
Incorrect |
39 ms |
4052 KB |
Output isn't correct |
51 |
Incorrect |
17 ms |
4752 KB |
Output isn't correct |
52 |
Incorrect |
20 ms |
4744 KB |
Output isn't correct |
53 |
Incorrect |
45 ms |
4712 KB |
Output isn't correct |
54 |
Incorrect |
19 ms |
5420 KB |
Output isn't correct |
55 |
Incorrect |
19 ms |
5420 KB |
Output isn't correct |
56 |
Incorrect |
56 ms |
5344 KB |
Output isn't correct |
57 |
Incorrect |
23 ms |
6044 KB |
Output isn't correct |
58 |
Incorrect |
23 ms |
6076 KB |
Output isn't correct |
59 |
Incorrect |
66 ms |
6064 KB |
Output isn't correct |
60 |
Incorrect |
30 ms |
6988 KB |
Output isn't correct |
61 |
Incorrect |
27 ms |
6848 KB |
Output isn't correct |
62 |
Incorrect |
80 ms |
6724 KB |
Output isn't correct |
63 |
Correct |
84 ms |
6732 KB |
Output is correct |
64 |
Correct |
106 ms |
6720 KB |
Output is correct |
65 |
Incorrect |
114 ms |
6724 KB |
Output isn't correct |
66 |
Incorrect |
86 ms |
6724 KB |
Output isn't correct |
67 |
Correct |
79 ms |
6720 KB |
Output is correct |
68 |
Incorrect |
45 ms |
6760 KB |
Output isn't correct |
69 |
Correct |
42 ms |
6764 KB |
Output is correct |
70 |
Correct |
41 ms |
6764 KB |
Output is correct |
71 |
Correct |
41 ms |
6796 KB |
Output is correct |
72 |
Incorrect |
38 ms |
6760 KB |
Output isn't correct |
73 |
Incorrect |
33 ms |
6772 KB |
Output isn't correct |
74 |
Correct |
39 ms |
6948 KB |
Output is correct |
75 |
Incorrect |
42 ms |
6676 KB |
Output isn't correct |
76 |
Incorrect |
43 ms |
6772 KB |
Output isn't correct |
77 |
Incorrect |
48 ms |
6716 KB |
Output isn't correct |
78 |
Correct |
50 ms |
6768 KB |
Output is correct |
79 |
Correct |
36 ms |
6780 KB |
Output is correct |
80 |
Incorrect |
42 ms |
6752 KB |
Output isn't correct |
81 |
Incorrect |
44 ms |
6788 KB |
Output isn't correct |
82 |
Incorrect |
40 ms |
6788 KB |
Output isn't correct |
83 |
Incorrect |
52 ms |
6756 KB |
Output isn't correct |
84 |
Correct |
44 ms |
6784 KB |
Output is correct |
85 |
Incorrect |
49 ms |
6796 KB |
Output isn't correct |
86 |
Incorrect |
48 ms |
6796 KB |
Output isn't correct |
87 |
Incorrect |
53 ms |
6764 KB |
Output isn't correct |
88 |
Incorrect |
50 ms |
6776 KB |
Output isn't correct |
89 |
Correct |
56 ms |
6984 KB |
Output is correct |
90 |
Correct |
54 ms |
6740 KB |
Output is correct |
91 |
Correct |
55 ms |
6736 KB |
Output is correct |
92 |
Incorrect |
52 ms |
6772 KB |
Output isn't correct |