#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
#define MOD (ll) (1e9+7)
#define pii pair<int,int>
#define ff first
#define ss second
int n,s;
pii start,finish;
string board[801];
vector<vector<int>> beedist(801, vector<int>(801, -1));
queue<pii> bees;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
bool inBounds(pii asdf) {
int x=asdf.ff; int y=asdf.ss;
return (x>=0 && x<n && y>=0 && y<n && board[x][y]!='T');
}
void beeDist() {
while(!bees.empty()) {
auto curr = bees.front(); bees.pop();
for(int i=0; i<4; i++) {
pii next = {curr.first+dx[i], curr.second+dy[i]};
if(inBounds(next) && beedist[next.ff][next.ss] == -1 && board[next.ff][next.ss]!='D') {
beedist[next.ff][next.ss] = beedist[curr.ff][curr.ss] + s;
bees.push(next);
}
}
}
}
bool test(int delay) {
vector<vector<int>> dist(801, vector<int>(801, -1));
if(delay*s>=beedist[start.ff][start.ss]) {
return false;
}
queue<pii> q; q.push(start);
dist[start.ff][start.ss] = delay*s;
while(!q.empty()) {
auto curr = q.front(); q.pop();
if(curr == finish) {
return true;
}
for(int i=0; i<4; i++) {
pii next = {curr.ff+dx[i], curr.ss+dy[i]};
if(inBounds(next)&&((dist[curr.ff][curr.ss]+1)<beedist[next.ff][next.ss])&&dist[next.ff][next.ss]==-1) {
dist[next.ff][next.ss] = dist[curr.ff][curr.ss]+1;
q.push(next);
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin>>n>>s;
for(int i=0; i<n; i++) {
cin>>board[i];
for(int j=0; j<n; j++) {
if(board[i][j] == 'M') {
start.ff = i;
start.ss = j;
} else if (board[i][j] == 'D') {
finish.ff = i;
finish.ss = j;
} else if (board[i][j] == 'H') {
beedist[i][j] = 0;
bees.push({i,j});
}
}
}
beeDist();
// for(int i=0; i<n; i++) {
// for(int j=0; j<n; j++) {
// cout<<beedist[i][j]<<' ';
// }
// cout<<endl;
// }
// cout<<test(0)<<endl;
// cout<<test(1)<<endl;
// cout<<test(2)<<endl;
// cout<<test(3)<<endl;
beedist[finish.ff][finish.ss] = n*n*s;
//binsearch :D
int lo = -1; int hi = 2*n*n;
while(hi-lo>1) {
int mid = (lo+hi)>>1;
if(test(mid)) {
lo = mid;
} else {
hi = mid;
}
}
cout<<lo<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5404 KB |
Output is correct |
2 |
Correct |
7 ms |
5424 KB |
Output is correct |
3 |
Correct |
7 ms |
5404 KB |
Output is correct |
4 |
Correct |
6 ms |
5432 KB |
Output is correct |
5 |
Correct |
8 ms |
5392 KB |
Output is correct |
6 |
Correct |
8 ms |
5424 KB |
Output is correct |
7 |
Correct |
74 ms |
6132 KB |
Output is correct |
8 |
Correct |
8 ms |
5424 KB |
Output is correct |
9 |
Correct |
8 ms |
5424 KB |
Output is correct |
10 |
Correct |
9 ms |
5404 KB |
Output is correct |
11 |
Correct |
8 ms |
5424 KB |
Output is correct |
12 |
Correct |
11 ms |
5460 KB |
Output is correct |
13 |
Correct |
11 ms |
5444 KB |
Output is correct |
14 |
Correct |
12 ms |
5444 KB |
Output is correct |
15 |
Correct |
9 ms |
5432 KB |
Output is correct |
16 |
Correct |
9 ms |
5444 KB |
Output is correct |
17 |
Correct |
9 ms |
5484 KB |
Output is correct |
18 |
Correct |
10 ms |
5444 KB |
Output is correct |
19 |
Correct |
10 ms |
5444 KB |
Output is correct |
20 |
Correct |
10 ms |
5484 KB |
Output is correct |
21 |
Correct |
13 ms |
5500 KB |
Output is correct |
22 |
Correct |
13 ms |
5460 KB |
Output is correct |
23 |
Correct |
11 ms |
5460 KB |
Output is correct |
24 |
Correct |
10 ms |
5460 KB |
Output is correct |
25 |
Correct |
12 ms |
5460 KB |
Output is correct |
26 |
Correct |
11 ms |
5440 KB |
Output is correct |
27 |
Correct |
15 ms |
5452 KB |
Output is correct |
28 |
Correct |
13 ms |
5468 KB |
Output is correct |
29 |
Correct |
12 ms |
5456 KB |
Output is correct |
30 |
Correct |
12 ms |
5456 KB |
Output is correct |
31 |
Correct |
12 ms |
5460 KB |
Output is correct |
32 |
Correct |
12 ms |
5460 KB |
Output is correct |
33 |
Correct |
15 ms |
5580 KB |
Output is correct |
34 |
Correct |
17 ms |
5604 KB |
Output is correct |
35 |
Correct |
29 ms |
5604 KB |
Output is correct |
36 |
Correct |
16 ms |
5640 KB |
Output is correct |
37 |
Correct |
18 ms |
5636 KB |
Output is correct |
38 |
Correct |
33 ms |
5640 KB |
Output is correct |
39 |
Correct |
20 ms |
5688 KB |
Output is correct |
40 |
Correct |
17 ms |
5692 KB |
Output is correct |
41 |
Correct |
40 ms |
5668 KB |
Output is correct |
42 |
Correct |
18 ms |
5736 KB |
Output is correct |
43 |
Correct |
19 ms |
5740 KB |
Output is correct |
44 |
Correct |
49 ms |
5704 KB |
Output is correct |
45 |
Correct |
20 ms |
5788 KB |
Output is correct |
46 |
Correct |
18 ms |
5760 KB |
Output is correct |
47 |
Correct |
52 ms |
5752 KB |
Output is correct |
48 |
Correct |
20 ms |
5936 KB |
Output is correct |
49 |
Correct |
21 ms |
5872 KB |
Output is correct |
50 |
Correct |
71 ms |
5876 KB |
Output is correct |
51 |
Correct |
20 ms |
5972 KB |
Output is correct |
52 |
Correct |
20 ms |
5940 KB |
Output is correct |
53 |
Correct |
84 ms |
5920 KB |
Output is correct |
54 |
Correct |
22 ms |
6020 KB |
Output is correct |
55 |
Correct |
21 ms |
6020 KB |
Output is correct |
56 |
Correct |
79 ms |
5988 KB |
Output is correct |
57 |
Correct |
22 ms |
6160 KB |
Output is correct |
58 |
Correct |
22 ms |
6056 KB |
Output is correct |
59 |
Correct |
98 ms |
6044 KB |
Output is correct |
60 |
Correct |
24 ms |
6156 KB |
Output is correct |
61 |
Correct |
23 ms |
6132 KB |
Output is correct |
62 |
Correct |
133 ms |
6092 KB |
Output is correct |
63 |
Correct |
99 ms |
6120 KB |
Output is correct |
64 |
Correct |
179 ms |
6108 KB |
Output is correct |
65 |
Correct |
157 ms |
6116 KB |
Output is correct |
66 |
Correct |
114 ms |
6116 KB |
Output is correct |
67 |
Correct |
108 ms |
6116 KB |
Output is correct |
68 |
Correct |
62 ms |
6124 KB |
Output is correct |
69 |
Correct |
53 ms |
6132 KB |
Output is correct |
70 |
Correct |
41 ms |
6152 KB |
Output is correct |
71 |
Correct |
48 ms |
6228 KB |
Output is correct |
72 |
Correct |
43 ms |
6152 KB |
Output is correct |
73 |
Correct |
35 ms |
6184 KB |
Output is correct |
74 |
Correct |
58 ms |
6176 KB |
Output is correct |
75 |
Correct |
70 ms |
6156 KB |
Output is correct |
76 |
Correct |
63 ms |
6152 KB |
Output is correct |
77 |
Correct |
58 ms |
6172 KB |
Output is correct |
78 |
Correct |
71 ms |
6148 KB |
Output is correct |
79 |
Correct |
48 ms |
6292 KB |
Output is correct |
80 |
Correct |
57 ms |
6168 KB |
Output is correct |
81 |
Correct |
64 ms |
6168 KB |
Output is correct |
82 |
Correct |
54 ms |
6152 KB |
Output is correct |
83 |
Correct |
70 ms |
6156 KB |
Output is correct |
84 |
Correct |
65 ms |
6144 KB |
Output is correct |
85 |
Correct |
72 ms |
6148 KB |
Output is correct |
86 |
Correct |
76 ms |
6244 KB |
Output is correct |
87 |
Correct |
67 ms |
6164 KB |
Output is correct |
88 |
Correct |
71 ms |
6128 KB |
Output is correct |
89 |
Correct |
72 ms |
6144 KB |
Output is correct |
90 |
Correct |
78 ms |
6184 KB |
Output is correct |
91 |
Correct |
85 ms |
6128 KB |
Output is correct |
92 |
Correct |
92 ms |
6208 KB |
Output is correct |