#include <bits/stdc++.h>
using namespace std;
struct node{
int i;
int j;
node(){}
node(int I,int J){
i=I;
j=J;
}
};
queue<node> Q;
char forest[805][805];
int bees[805][805];
int dist[805][805];
int energy[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int n,s;
void bfsBees(){
while(!Q.empty()){
node u=Q.front();
Q.pop();
for(int mov=0;mov<4;mov++){
int i=u.i+dx[mov];
int j=u.j+dy[mov];
if(i<1 || i>n) continue;
if(j<1 || j>n) continue;
if(forest[i][j]=='T' || forest[i][j]=='D') continue;
if(bees[i][j]==INT_MAX){
bees[i][j]=bees[u.i][u.j]+1;
Q.push(node(i,j));
}
}
}
}
node mecho;
node home;
bool bfsMecho(int init){
if(bees[mecho.i][mecho.j]<=init) return false;
dist[mecho.i][mecho.j]=init;
energy[mecho.i][mecho.j]=0;
Q.push(mecho);
while(!Q.empty()){
node u=Q.front();
Q.pop();
if(bees[u.i][u.j]==dist[u.i][u.j] && energy[u.i][u.j]==0) continue;
for(int mov=0;mov<4;mov++){
int i=u.i+dx[mov];
int j=u.j+dy[mov];
if(i<1 || i>n) continue;
if(j<1 || j>n) continue;
if(forest[i][j]=='T') continue;
if(dist[i][j]==INT_MAX){
int change=(energy[u.i][u.j]>0)? 0 : 1;
dist[i][j]=dist[u.i][u.j]+change;
if(bees[i][j]<dist[i][j]) continue;
energy[i][j]=(energy[u.i][u.j]>0)? energy[u.i][u.j]-1 : s-1;
Q.push(node(i,j));
}
}
}
return dist[home.i][home.j]!=INT_MAX;
}
int binarySearch(){
int k=-1;
int L=0,R=3*n;
while(L<R){
int mid=(L+R)/2;
if(bfsMecho(mid)){
k=mid;
L=mid+1;
}
else{
R=mid;
}
}
return k;
}
int binarySearch2(){
int k=-1;
for(int b=5000;b>=1;b/=2){
while(bfsMecho(b+k)) k+=b;
}
return k;
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>s;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>forest[i][j]; dist[i][j]=INT_MAX;
energy[i][j]=0;
if(forest[i][j]=='M'){
mecho=node(i,j);
}
else if(forest[i][j]=='D'){
home=node(i,j);
} bees[i][j]=INT_MAX;
if(forest[i][j]=='H'){
Q.push(node(i,j));
bees[i][j]=0; }
}
}
bfsBees();
int ans=binarySearch2();
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
5 |
Incorrect |
1 ms |
512 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
7 |
Incorrect |
36 ms |
9324 KB |
Output isn't correct |
8 |
Correct |
1 ms |
492 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
11 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
12 |
Correct |
1 ms |
1004 KB |
Output is correct |
13 |
Correct |
1 ms |
876 KB |
Output is correct |
14 |
Correct |
1 ms |
1024 KB |
Output is correct |
15 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
19 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
620 KB |
Output isn't correct |
21 |
Incorrect |
1 ms |
748 KB |
Output isn't correct |
22 |
Incorrect |
1 ms |
748 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
876 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
876 KB |
Output isn't correct |
25 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
26 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
27 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
28 |
Incorrect |
1 ms |
1004 KB |
Output isn't correct |
29 |
Incorrect |
1 ms |
1132 KB |
Output isn't correct |
30 |
Incorrect |
1 ms |
1132 KB |
Output isn't correct |
31 |
Incorrect |
1 ms |
1132 KB |
Output isn't correct |
32 |
Incorrect |
1 ms |
1132 KB |
Output isn't correct |
33 |
Incorrect |
7 ms |
4076 KB |
Output isn't correct |
34 |
Incorrect |
7 ms |
4076 KB |
Output isn't correct |
35 |
Incorrect |
9 ms |
4076 KB |
Output isn't correct |
36 |
Incorrect |
8 ms |
4588 KB |
Output isn't correct |
37 |
Incorrect |
9 ms |
4588 KB |
Output isn't correct |
38 |
Incorrect |
11 ms |
4588 KB |
Output isn't correct |
39 |
Incorrect |
10 ms |
5100 KB |
Output isn't correct |
40 |
Incorrect |
11 ms |
5100 KB |
Output isn't correct |
41 |
Incorrect |
14 ms |
5228 KB |
Output isn't correct |
42 |
Incorrect |
12 ms |
5740 KB |
Output isn't correct |
43 |
Incorrect |
13 ms |
5740 KB |
Output isn't correct |
44 |
Incorrect |
17 ms |
5740 KB |
Output isn't correct |
45 |
Incorrect |
14 ms |
6252 KB |
Output isn't correct |
46 |
Incorrect |
15 ms |
6252 KB |
Output isn't correct |
47 |
Incorrect |
21 ms |
6252 KB |
Output isn't correct |
48 |
Incorrect |
16 ms |
6892 KB |
Output isn't correct |
49 |
Incorrect |
18 ms |
6892 KB |
Output isn't correct |
50 |
Incorrect |
24 ms |
6892 KB |
Output isn't correct |
51 |
Incorrect |
20 ms |
7404 KB |
Output isn't correct |
52 |
Incorrect |
20 ms |
7404 KB |
Output isn't correct |
53 |
Incorrect |
26 ms |
7404 KB |
Output isn't correct |
54 |
Incorrect |
22 ms |
8044 KB |
Output isn't correct |
55 |
Incorrect |
23 ms |
8044 KB |
Output isn't correct |
56 |
Incorrect |
30 ms |
8044 KB |
Output isn't correct |
57 |
Incorrect |
25 ms |
8556 KB |
Output isn't correct |
58 |
Incorrect |
26 ms |
8556 KB |
Output isn't correct |
59 |
Incorrect |
35 ms |
8556 KB |
Output isn't correct |
60 |
Incorrect |
29 ms |
9116 KB |
Output isn't correct |
61 |
Incorrect |
29 ms |
9068 KB |
Output isn't correct |
62 |
Incorrect |
40 ms |
9068 KB |
Output isn't correct |
63 |
Incorrect |
34 ms |
9068 KB |
Output isn't correct |
64 |
Incorrect |
33 ms |
9068 KB |
Output isn't correct |
65 |
Incorrect |
33 ms |
9068 KB |
Output isn't correct |
66 |
Incorrect |
35 ms |
9068 KB |
Output isn't correct |
67 |
Correct |
33 ms |
9068 KB |
Output is correct |
68 |
Incorrect |
37 ms |
9068 KB |
Output isn't correct |
69 |
Incorrect |
36 ms |
9068 KB |
Output isn't correct |
70 |
Incorrect |
36 ms |
9068 KB |
Output isn't correct |
71 |
Incorrect |
36 ms |
9068 KB |
Output isn't correct |
72 |
Correct |
36 ms |
9068 KB |
Output is correct |
73 |
Correct |
42 ms |
9324 KB |
Output is correct |
74 |
Incorrect |
34 ms |
9452 KB |
Output isn't correct |
75 |
Incorrect |
35 ms |
9324 KB |
Output isn't correct |
76 |
Incorrect |
35 ms |
9452 KB |
Output isn't correct |
77 |
Incorrect |
37 ms |
9324 KB |
Output isn't correct |
78 |
Correct |
35 ms |
9324 KB |
Output is correct |
79 |
Incorrect |
41 ms |
9324 KB |
Output isn't correct |
80 |
Incorrect |
34 ms |
9324 KB |
Output isn't correct |
81 |
Incorrect |
35 ms |
9324 KB |
Output isn't correct |
82 |
Incorrect |
35 ms |
9324 KB |
Output isn't correct |
83 |
Incorrect |
40 ms |
9324 KB |
Output isn't correct |
84 |
Incorrect |
35 ms |
9324 KB |
Output isn't correct |
85 |
Incorrect |
35 ms |
9452 KB |
Output isn't correct |
86 |
Incorrect |
34 ms |
9324 KB |
Output isn't correct |
87 |
Incorrect |
36 ms |
9324 KB |
Output isn't correct |
88 |
Incorrect |
34 ms |
9196 KB |
Output isn't correct |
89 |
Incorrect |
41 ms |
9196 KB |
Output isn't correct |
90 |
Incorrect |
35 ms |
9196 KB |
Output isn't correct |
91 |
Incorrect |
35 ms |
9196 KB |
Output isn't correct |
92 |
Incorrect |
34 ms |
9196 KB |
Output isn't correct |