// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <class T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define pb push_back
#define ll long long
#define nl '\n'
/*
Mecho can only walk on grass and cannot go through trees or hives, and he can make at most S steps per minute.
every minute
he eats or
he leaves immediately and takes up to S steps through the forest as described above
bees spread to grassy cells
trees = no one can go there
m = mecho, grassy cells => i will replace with g
g = grassy
d = mecho home; bees cannot go / pass here
h = hive
*/
const int N = 810;
char grid[N][N];
int stepsMecho[N][N], stepsBee[N][N];
int n, s;
array<int, 2> Mecho, MechoHouse;
vector<array<int, 2>> Bee;
vector<array<int,2>> moves = {{-1,0},{1,0},{0,-1},{0,1}};
struct m{
bool isMecho;
int x,y;
};
bool good(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < n && grid[x][y] != 'T';
}
bool isHome(int x, int y) {
return x == MechoHouse[0] && y == MechoHouse[1];
}
void debug() {
cout << "Mecho: " << nl;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cout << stepsMecho[i][j] << " ";
}
cout << nl;
}
cout << "Bees: " << nl;
for (int i=0;i<n;i++) {
for (int j=0;j<n;j++) {
cout << stepsBee[i][j] << " ";
}
cout << nl;
}
}
bool bs(int steps) {
memset(stepsMecho,-1,sizeof(stepsMecho));
memset(stepsBee,-1,sizeof(stepsBee));
queue<m> q, q2;
q2.push(m{true,Mecho[0],Mecho[1]});
stepsMecho[Mecho[0]][Mecho[1]] = 0;
for (auto [x,y]:Bee) {
q.push(m{false,x,y});
stepsBee[x][y]=0;
}
while(!q.empty()) {
m v = q.front(); q.pop();
if (stepsBee[v.x][v.y] == steps) { q2.push(v); continue; }
for (auto [addx,addy]: moves) {
int nx = v.x + addx, ny = v.y + addy;
if (good(nx,ny) && !isHome(nx,ny) && stepsBee[nx][ny] == -1) {
q.push(m{false,nx,ny});
stepsBee[nx][ny] = stepsBee[v.x][v.y]+1;
}
}
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
if (stepsBee[i][j] != -1)
stepsBee[i][j] = 0;
while(!q2.empty()) {
m v = q2.front(); q2.pop();
if (v.isMecho) {
if (stepsBee[v.x][v.y] != -1 && stepsBee[v.x][v.y] <= (stepsMecho[v.x][v.y]+s-1)/s) { continue; }
for (auto [addx,addy]: moves) {
int nx = v.x + addx, ny = v.y + addy;
if (good(nx,ny) && stepsMecho[nx][ny] == -1 && (stepsBee[nx][ny] == -1 || stepsBee[nx][ny] > (stepsMecho[v.x][v.y]+s)/s)) {
if (isHome(nx,ny)) { return 1; }
stepsMecho[nx][ny] = stepsMecho[v.x][v.y]+1;
q2.push(m{true,nx,ny});
}
}
} else {
for (auto [addx,addy]: moves) {
int nx = v.x + addx, ny = v.y + addy;
if (good(nx,ny) && !isHome(nx,ny) && stepsBee[nx][ny] == -1) {
q2.push(m{false,nx,ny});
stepsBee[nx][ny] = stepsBee[v.x][v.y]+1;
}
}
}
}
return 0;
}
signed main() {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> s;
for (int i=0;i<n;i++)
for (int j=0;j<n;j++) {
cin >> grid[i][j];
if (grid[i][j]=='M') {
grid[i][j] = 'G';
Mecho = {i,j};
}
if (grid[i][j]=='H') {
Bee.pb({i,j});
}
if (grid[i][j]=='D') {
MechoHouse = {i,j};
}
}
int lo = 0, hi = 1e9;
while(lo<=hi) {
int mid = (lo+hi)/2;
if (bs(mid)) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
cout << lo;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5468 KB |
Output isn't correct |
2 |
Incorrect |
7 ms |
5468 KB |
Output isn't correct |
3 |
Incorrect |
7 ms |
5460 KB |
Output isn't correct |
4 |
Incorrect |
7 ms |
5460 KB |
Output isn't correct |
5 |
Correct |
7 ms |
5452 KB |
Output is correct |
6 |
Correct |
7 ms |
5460 KB |
Output is correct |
7 |
Correct |
550 ms |
6880 KB |
Output is correct |
8 |
Incorrect |
6 ms |
5460 KB |
Output isn't correct |
9 |
Correct |
10 ms |
5452 KB |
Output is correct |
10 |
Correct |
7 ms |
5460 KB |
Output is correct |
11 |
Correct |
9 ms |
5452 KB |
Output is correct |
12 |
Incorrect |
8 ms |
5456 KB |
Output isn't correct |
13 |
Incorrect |
10 ms |
5460 KB |
Output isn't correct |
14 |
Incorrect |
12 ms |
5524 KB |
Output isn't correct |
15 |
Incorrect |
11 ms |
5588 KB |
Output isn't correct |
16 |
Correct |
10 ms |
5360 KB |
Output is correct |
17 |
Incorrect |
7 ms |
5452 KB |
Output isn't correct |
18 |
Correct |
7 ms |
5480 KB |
Output is correct |
19 |
Incorrect |
7 ms |
5460 KB |
Output isn't correct |
20 |
Correct |
8 ms |
5488 KB |
Output is correct |
21 |
Incorrect |
7 ms |
5460 KB |
Output isn't correct |
22 |
Correct |
7 ms |
5460 KB |
Output is correct |
23 |
Incorrect |
7 ms |
5448 KB |
Output isn't correct |
24 |
Correct |
8 ms |
5460 KB |
Output is correct |
25 |
Incorrect |
8 ms |
5460 KB |
Output isn't correct |
26 |
Correct |
10 ms |
5448 KB |
Output is correct |
27 |
Incorrect |
8 ms |
5524 KB |
Output isn't correct |
28 |
Correct |
8 ms |
5452 KB |
Output is correct |
29 |
Incorrect |
9 ms |
5460 KB |
Output isn't correct |
30 |
Correct |
8 ms |
5460 KB |
Output is correct |
31 |
Incorrect |
13 ms |
5512 KB |
Output isn't correct |
32 |
Correct |
9 ms |
5528 KB |
Output is correct |
33 |
Incorrect |
50 ms |
5848 KB |
Output isn't correct |
34 |
Correct |
45 ms |
5844 KB |
Output is correct |
35 |
Correct |
82 ms |
5932 KB |
Output is correct |
36 |
Incorrect |
62 ms |
5844 KB |
Output isn't correct |
37 |
Correct |
66 ms |
5844 KB |
Output is correct |
38 |
Correct |
108 ms |
5928 KB |
Output is correct |
39 |
Incorrect |
79 ms |
5996 KB |
Output isn't correct |
40 |
Correct |
69 ms |
5996 KB |
Output is correct |
41 |
Correct |
136 ms |
6008 KB |
Output is correct |
42 |
Incorrect |
96 ms |
6100 KB |
Output isn't correct |
43 |
Correct |
85 ms |
6100 KB |
Output is correct |
44 |
Correct |
165 ms |
6100 KB |
Output is correct |
45 |
Incorrect |
111 ms |
6172 KB |
Output isn't correct |
46 |
Correct |
105 ms |
6176 KB |
Output is correct |
47 |
Correct |
198 ms |
6196 KB |
Output is correct |
48 |
Incorrect |
136 ms |
6228 KB |
Output isn't correct |
49 |
Correct |
122 ms |
6228 KB |
Output is correct |
50 |
Correct |
246 ms |
6304 KB |
Output is correct |
51 |
Incorrect |
162 ms |
6372 KB |
Output isn't correct |
52 |
Correct |
147 ms |
6376 KB |
Output is correct |
53 |
Correct |
278 ms |
6516 KB |
Output is correct |
54 |
Incorrect |
185 ms |
6484 KB |
Output isn't correct |
55 |
Correct |
163 ms |
6484 KB |
Output is correct |
56 |
Correct |
334 ms |
6496 KB |
Output is correct |
57 |
Incorrect |
205 ms |
6484 KB |
Output isn't correct |
58 |
Correct |
184 ms |
6520 KB |
Output is correct |
59 |
Correct |
368 ms |
6624 KB |
Output is correct |
60 |
Incorrect |
233 ms |
6704 KB |
Output isn't correct |
61 |
Correct |
210 ms |
6612 KB |
Output is correct |
62 |
Correct |
416 ms |
6728 KB |
Output is correct |
63 |
Correct |
636 ms |
6720 KB |
Output is correct |
64 |
Correct |
701 ms |
6848 KB |
Output is correct |
65 |
Correct |
715 ms |
6852 KB |
Output is correct |
66 |
Incorrect |
658 ms |
6612 KB |
Output isn't correct |
67 |
Incorrect |
630 ms |
6716 KB |
Output isn't correct |
68 |
Correct |
629 ms |
6764 KB |
Output is correct |
69 |
Correct |
638 ms |
6768 KB |
Output is correct |
70 |
Correct |
631 ms |
6764 KB |
Output is correct |
71 |
Correct |
608 ms |
6764 KB |
Output is correct |
72 |
Incorrect |
651 ms |
6764 KB |
Output isn't correct |
73 |
Incorrect |
511 ms |
7180 KB |
Output isn't correct |
74 |
Correct |
539 ms |
7156 KB |
Output is correct |
75 |
Correct |
562 ms |
7156 KB |
Output is correct |
76 |
Correct |
533 ms |
7160 KB |
Output is correct |
77 |
Correct |
544 ms |
7256 KB |
Output is correct |
78 |
Incorrect |
555 ms |
7116 KB |
Output isn't correct |
79 |
Correct |
559 ms |
7100 KB |
Output is correct |
80 |
Correct |
569 ms |
7124 KB |
Output is correct |
81 |
Correct |
559 ms |
7108 KB |
Output is correct |
82 |
Correct |
564 ms |
7112 KB |
Output is correct |
83 |
Correct |
548 ms |
6984 KB |
Output is correct |
84 |
Correct |
579 ms |
7052 KB |
Output is correct |
85 |
Correct |
588 ms |
7040 KB |
Output is correct |
86 |
Correct |
598 ms |
7040 KB |
Output is correct |
87 |
Correct |
576 ms |
7116 KB |
Output is correct |
88 |
Correct |
544 ms |
7092 KB |
Output is correct |
89 |
Correct |
570 ms |
6988 KB |
Output is correct |
90 |
Correct |
587 ms |
6952 KB |
Output is correct |
91 |
Correct |
688 ms |
6964 KB |
Output is correct |
92 |
Correct |
640 ms |
6984 KB |
Output is correct |