#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;
}
Compilation message
mecho.cpp: In function 'bool check(int)':
mecho.cpp:38:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
38 | auto [i, j] = pq.front(); pq.pop();
| ^
mecho.cpp: In function 'int main()':
mecho.cpp:96:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | auto [i, j] = pq.front();
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
444 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
70 ms |
9044 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
952 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 |
0 ms |
700 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 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 |
1 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 |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
2 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
1116 KB |
Output is correct |
32 |
Correct |
1 ms |
1116 KB |
Output is correct |
33 |
Correct |
7 ms |
4104 KB |
Output is correct |
34 |
Correct |
8 ms |
3928 KB |
Output is correct |
35 |
Correct |
18 ms |
3932 KB |
Output is correct |
36 |
Correct |
9 ms |
4568 KB |
Output is correct |
37 |
Correct |
10 ms |
4700 KB |
Output is correct |
38 |
Correct |
25 ms |
4696 KB |
Output is correct |
39 |
Correct |
12 ms |
5036 KB |
Output is correct |
40 |
Correct |
12 ms |
5212 KB |
Output is correct |
41 |
Correct |
32 ms |
5016 KB |
Output is correct |
42 |
Correct |
14 ms |
5724 KB |
Output is correct |
43 |
Correct |
15 ms |
5636 KB |
Output is correct |
44 |
Correct |
45 ms |
5728 KB |
Output is correct |
45 |
Correct |
17 ms |
6240 KB |
Output is correct |
46 |
Correct |
16 ms |
6276 KB |
Output is correct |
47 |
Correct |
42 ms |
6188 KB |
Output is correct |
48 |
Correct |
20 ms |
7000 KB |
Output is correct |
49 |
Correct |
21 ms |
6732 KB |
Output is correct |
50 |
Correct |
50 ms |
6748 KB |
Output is correct |
51 |
Correct |
23 ms |
7516 KB |
Output is correct |
52 |
Correct |
24 ms |
7516 KB |
Output is correct |
53 |
Correct |
65 ms |
7504 KB |
Output is correct |
54 |
Correct |
26 ms |
7856 KB |
Output is correct |
55 |
Correct |
26 ms |
7880 KB |
Output is correct |
56 |
Correct |
83 ms |
8028 KB |
Output is correct |
57 |
Correct |
30 ms |
8528 KB |
Output is correct |
58 |
Correct |
31 ms |
8416 KB |
Output is correct |
59 |
Correct |
83 ms |
8532 KB |
Output is correct |
60 |
Correct |
33 ms |
9040 KB |
Output is correct |
61 |
Correct |
34 ms |
9144 KB |
Output is correct |
62 |
Correct |
89 ms |
8924 KB |
Output is correct |
63 |
Correct |
85 ms |
9040 KB |
Output is correct |
64 |
Correct |
150 ms |
9040 KB |
Output is correct |
65 |
Correct |
121 ms |
8948 KB |
Output is correct |
66 |
Correct |
98 ms |
8960 KB |
Output is correct |
67 |
Correct |
93 ms |
9148 KB |
Output is correct |
68 |
Correct |
47 ms |
9168 KB |
Output is correct |
69 |
Correct |
49 ms |
9040 KB |
Output is correct |
70 |
Correct |
42 ms |
9044 KB |
Output is correct |
71 |
Correct |
43 ms |
9044 KB |
Output is correct |
72 |
Correct |
39 ms |
9000 KB |
Output is correct |
73 |
Correct |
41 ms |
9304 KB |
Output is correct |
74 |
Correct |
56 ms |
9304 KB |
Output is correct |
75 |
Correct |
68 ms |
9300 KB |
Output is correct |
76 |
Correct |
102 ms |
9296 KB |
Output is correct |
77 |
Correct |
60 ms |
9268 KB |
Output is correct |
78 |
Correct |
105 ms |
9196 KB |
Output is correct |
79 |
Correct |
84 ms |
9296 KB |
Output is correct |
80 |
Correct |
87 ms |
9300 KB |
Output is correct |
81 |
Correct |
65 ms |
9296 KB |
Output is correct |
82 |
Correct |
58 ms |
9300 KB |
Output is correct |
83 |
Correct |
68 ms |
9156 KB |
Output is correct |
84 |
Correct |
89 ms |
9144 KB |
Output is correct |
85 |
Correct |
86 ms |
9200 KB |
Output is correct |
86 |
Correct |
79 ms |
9316 KB |
Output is correct |
87 |
Correct |
97 ms |
9372 KB |
Output is correct |
88 |
Correct |
77 ms |
9300 KB |
Output is correct |
89 |
Correct |
72 ms |
9316 KB |
Output is correct |
90 |
Correct |
80 ms |
9084 KB |
Output is correct |
91 |
Correct |
82 ms |
9072 KB |
Output is correct |
92 |
Correct |
88 ms |
9072 KB |
Output is correct |