#include <bits/stdc++.h>
using namespace std;
int n, s, mx, my, ex, ey;
const int MAXN = 807;
char grid[MAXN][MAXN];
int bees[MAXN][MAXN];
int mecho[MAXN][MAXN];
int visited[MAXN][MAXN];
int d1[] = {0, 1, 0, -1};
int d2[] = {1, 0, -1, 0};
bool valid(int dx, int dy){
return dx >= 0 && dx < n && dy >= 0 && dy < n && (grid[dx][dy] == 'G' || grid[dx][dy] == 'M');
}
bool chega(int m, int b){
return ((m/s) < b);
}
bool check(int d){
if(bees[mx][my] <= d) return false;
queue<pair<int, int>> pq;
pq.push({mx, my});
//reset
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
visited[i][j] = 0;
mecho[i][j] = 0;
}
}
visited[mx][my] = 1;
while(!pq.empty()){
auto [i, j] = pq.front(); pq.pop();
for(int x = 0; x < 4; x++){
int dx = d1[x] + i;
int dy = d2[x] + j;
if(valid(dx, dy) && !visited[dx][dy] && chega(mecho[i][j] + 1, bees[dx][dy] - d)){
mecho[dx][dy] = mecho[i][j] + 1;
visited[dx][dy] = 1;
pq.push({dx, dy});
}
}
}
// cout << "mecho here" << endl;
// for(int i = 0; i < n; i++){
// for(int j = 0; j < n; j++){
// cout << mecho[i][j] << " ";
// }
// cout << endl;
// }
bool res = false;
for(int i = 0; i < 4; i++){
int dx = d1[i] + ex;
int dy = d2[i] + ey;
if(valid(dx, dy) && chega(mecho[dx][dy], bees[dx][dy]) && visited[dx][dy]){
res = true;
}
}
return res;
}
int main(){
cin >> n >> s;
vector<pair<int, int>> h;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> grid[i][j];
if(grid[i][j] == 'H')
h.push_back({i, j});
else if(grid[i][j] == 'M'){
mx = i;
my = j;
}else if(grid[i][j] == 'D'){
ex = i;
ey = j;
}
}
}
//bfs para bees
//t, i, j
queue<pair<int, int>> pq;
for(auto u : h) pq.push({u.first, u.second});
//funcionando o bfs para as abelhas!!
while(!pq.empty()){
auto [i, j] = pq.front();
pq.pop();
for(int x = 0; x < 4; x++){
int dx = d1[x] + i;
int dy = d2[x] + j;
if(valid(dx, dy) && !bees[dx][dy]){
bees[dx][dy] = bees[i][j] + 1;
pq.push({dx, dy});
}
}
}
// for(int i = 0; i < n; i++){
// for(int j = 0; j < n; j++){
// cout << bees[i][j] << " ";
// }
// cout << endl;
// }
int l = 0, r = n*n;
while(l <= r){
//cout << l << " " << r << endl;
int mid = (l+r)/2;
if(check(mid)) l = mid+1;
else r = mid-1;
}
cout << l-1 << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
104 ms |
8612 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
860 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
0 ms |
860 KB |
Output is correct |
25 |
Correct |
1 ms |
860 KB |
Output is correct |
26 |
Correct |
1 ms |
860 KB |
Output is correct |
27 |
Correct |
1 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1112 KB |
Output is correct |
29 |
Correct |
1 ms |
1112 KB |
Output is correct |
30 |
Correct |
1 ms |
1164 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
8 ms |
3932 KB |
Output is correct |
34 |
Correct |
8 ms |
3960 KB |
Output is correct |
35 |
Correct |
31 ms |
3932 KB |
Output is correct |
36 |
Correct |
9 ms |
4444 KB |
Output is correct |
37 |
Correct |
10 ms |
4428 KB |
Output is correct |
38 |
Correct |
27 ms |
4444 KB |
Output is correct |
39 |
Correct |
11 ms |
4952 KB |
Output is correct |
40 |
Correct |
12 ms |
4952 KB |
Output is correct |
41 |
Correct |
33 ms |
4816 KB |
Output is correct |
42 |
Correct |
14 ms |
5468 KB |
Output is correct |
43 |
Correct |
21 ms |
5468 KB |
Output is correct |
44 |
Correct |
36 ms |
5468 KB |
Output is correct |
45 |
Correct |
19 ms |
5972 KB |
Output is correct |
46 |
Correct |
17 ms |
5980 KB |
Output is correct |
47 |
Correct |
45 ms |
5956 KB |
Output is correct |
48 |
Correct |
20 ms |
6492 KB |
Output is correct |
49 |
Correct |
19 ms |
6428 KB |
Output is correct |
50 |
Correct |
50 ms |
6596 KB |
Output is correct |
51 |
Correct |
22 ms |
7004 KB |
Output is correct |
52 |
Correct |
22 ms |
6900 KB |
Output is correct |
53 |
Correct |
67 ms |
6892 KB |
Output is correct |
54 |
Correct |
25 ms |
7504 KB |
Output is correct |
55 |
Correct |
28 ms |
7516 KB |
Output is correct |
56 |
Correct |
86 ms |
7612 KB |
Output is correct |
57 |
Correct |
31 ms |
8020 KB |
Output is correct |
58 |
Correct |
31 ms |
8016 KB |
Output is correct |
59 |
Correct |
84 ms |
8132 KB |
Output is correct |
60 |
Correct |
33 ms |
8528 KB |
Output is correct |
61 |
Correct |
36 ms |
8476 KB |
Output is correct |
62 |
Correct |
96 ms |
8636 KB |
Output is correct |
63 |
Correct |
89 ms |
8540 KB |
Output is correct |
64 |
Correct |
159 ms |
8532 KB |
Output is correct |
65 |
Correct |
121 ms |
8484 KB |
Output is correct |
66 |
Correct |
104 ms |
8636 KB |
Output is correct |
67 |
Correct |
97 ms |
8540 KB |
Output is correct |
68 |
Correct |
49 ms |
8532 KB |
Output is correct |
69 |
Correct |
47 ms |
8540 KB |
Output is correct |
70 |
Correct |
40 ms |
8484 KB |
Output is correct |
71 |
Correct |
44 ms |
8484 KB |
Output is correct |
72 |
Correct |
36 ms |
8476 KB |
Output is correct |
73 |
Correct |
41 ms |
8756 KB |
Output is correct |
74 |
Correct |
51 ms |
8796 KB |
Output is correct |
75 |
Correct |
59 ms |
8936 KB |
Output is correct |
76 |
Correct |
53 ms |
8788 KB |
Output is correct |
77 |
Correct |
53 ms |
8784 KB |
Output is correct |
78 |
Correct |
79 ms |
8788 KB |
Output is correct |
79 |
Correct |
46 ms |
8788 KB |
Output is correct |
80 |
Correct |
57 ms |
8740 KB |
Output is correct |
81 |
Correct |
58 ms |
8792 KB |
Output is correct |
82 |
Correct |
56 ms |
8900 KB |
Output is correct |
83 |
Correct |
78 ms |
8796 KB |
Output is correct |
84 |
Correct |
63 ms |
8860 KB |
Output is correct |
85 |
Correct |
64 ms |
8796 KB |
Output is correct |
86 |
Correct |
74 ms |
8764 KB |
Output is correct |
87 |
Correct |
70 ms |
8788 KB |
Output is correct |
88 |
Correct |
66 ms |
8796 KB |
Output is correct |
89 |
Correct |
68 ms |
8780 KB |
Output is correct |
90 |
Correct |
70 ms |
8796 KB |
Output is correct |
91 |
Correct |
69 ms |
8788 KB |
Output is correct |
92 |
Correct |
76 ms |
8788 KB |
Output is correct |