#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;
}
}
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 |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
86 ms |
9296 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Incorrect |
1 ms |
860 KB |
Output isn't 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 |
604 KB |
Output is correct |
18 |
Correct |
0 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 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 |
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 |
1 ms |
964 KB |
Output is correct |
29 |
Correct |
1 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 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 |
3932 KB |
Output is correct |
34 |
Correct |
7 ms |
3932 KB |
Output is correct |
35 |
Correct |
18 ms |
3976 KB |
Output is correct |
36 |
Correct |
10 ms |
4700 KB |
Output is correct |
37 |
Correct |
10 ms |
4700 KB |
Output is correct |
38 |
Correct |
23 ms |
4724 KB |
Output is correct |
39 |
Correct |
11 ms |
5232 KB |
Output is correct |
40 |
Correct |
11 ms |
5208 KB |
Output is correct |
41 |
Correct |
31 ms |
5212 KB |
Output is correct |
42 |
Correct |
14 ms |
5724 KB |
Output is correct |
43 |
Correct |
15 ms |
5724 KB |
Output is correct |
44 |
Correct |
38 ms |
5676 KB |
Output is correct |
45 |
Correct |
17 ms |
6236 KB |
Output is correct |
46 |
Correct |
17 ms |
6388 KB |
Output is correct |
47 |
Correct |
46 ms |
6236 KB |
Output is correct |
48 |
Correct |
19 ms |
6748 KB |
Output is correct |
49 |
Correct |
22 ms |
6744 KB |
Output is correct |
50 |
Correct |
51 ms |
6952 KB |
Output is correct |
51 |
Correct |
24 ms |
7516 KB |
Output is correct |
52 |
Correct |
25 ms |
7480 KB |
Output is correct |
53 |
Correct |
66 ms |
7524 KB |
Output is correct |
54 |
Correct |
26 ms |
7868 KB |
Output is correct |
55 |
Correct |
25 ms |
8028 KB |
Output is correct |
56 |
Correct |
79 ms |
8020 KB |
Output is correct |
57 |
Correct |
27 ms |
8644 KB |
Output is correct |
58 |
Correct |
31 ms |
8536 KB |
Output is correct |
59 |
Correct |
91 ms |
8540 KB |
Output is correct |
60 |
Correct |
33 ms |
9052 KB |
Output is correct |
61 |
Correct |
33 ms |
9052 KB |
Output is correct |
62 |
Correct |
107 ms |
9040 KB |
Output is correct |
63 |
Correct |
89 ms |
9036 KB |
Output is correct |
64 |
Correct |
149 ms |
9044 KB |
Output is correct |
65 |
Correct |
121 ms |
9044 KB |
Output is correct |
66 |
Correct |
104 ms |
9044 KB |
Output is correct |
67 |
Correct |
99 ms |
9264 KB |
Output is correct |
68 |
Correct |
52 ms |
9312 KB |
Output is correct |
69 |
Correct |
52 ms |
9292 KB |
Output is correct |
70 |
Correct |
43 ms |
9040 KB |
Output is correct |
71 |
Correct |
46 ms |
9044 KB |
Output is correct |
72 |
Correct |
41 ms |
9040 KB |
Output is correct |
73 |
Correct |
38 ms |
9552 KB |
Output is correct |
74 |
Correct |
61 ms |
9500 KB |
Output is correct |
75 |
Correct |
61 ms |
9556 KB |
Output is correct |
76 |
Correct |
56 ms |
9552 KB |
Output is correct |
77 |
Correct |
60 ms |
9568 KB |
Output is correct |
78 |
Correct |
65 ms |
9520 KB |
Output is correct |
79 |
Correct |
50 ms |
9300 KB |
Output is correct |
80 |
Correct |
55 ms |
9520 KB |
Output is correct |
81 |
Correct |
59 ms |
9300 KB |
Output is correct |
82 |
Correct |
64 ms |
9296 KB |
Output is correct |
83 |
Correct |
67 ms |
9320 KB |
Output is correct |
84 |
Correct |
63 ms |
9484 KB |
Output is correct |
85 |
Correct |
67 ms |
9300 KB |
Output is correct |
86 |
Correct |
66 ms |
9524 KB |
Output is correct |
87 |
Correct |
68 ms |
9308 KB |
Output is correct |
88 |
Correct |
66 ms |
9300 KB |
Output is correct |
89 |
Correct |
88 ms |
9428 KB |
Output is correct |
90 |
Correct |
75 ms |
9316 KB |
Output is correct |
91 |
Correct |
87 ms |
9296 KB |
Output is correct |
92 |
Correct |
78 ms |
9296 KB |
Output is correct |