#include <bits/stdc++.h>
using namespace std;
///Binary Search
///BFS
///Determine the number of minutes that Mecho can stay eating honey (moment he leaves -1)
///Use two BFS, for the bees and for Mecho, second has parameter "fuel" since he can travel more than 1 step per minute
struct node
{
int i,j;
node(){}
node(int i,int j) : i(i),j(j){}
};
int n,s;
char board[805][805];
int bee[805][805];
int d[805][805];
int color[805][805];
int fuel[805][805];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
queue<node> Q;
node start,finish;
void bfsBee()
{
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
bee[i][j]=INT_MAX;
if(board[i][j]=='H'){
Q.push(node(i,j));
bee[i][j]=0;
color[i][j]=-1;
}
}
}
while(Q.size()){
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(board[i][j]=='T' || board[i][j]=='D') continue;
if(color[i][j]==0){
color[i][j]=-1;
Q.push(node(i,j));
bee[i][j]=bee[u.i][u.j]+1;
}
}
}
}
void bfsMecho(int init_d)
{
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j]=INT_MAX;
color[i][j]=0;
}
}
if(bee[start.i][start.j]<=init_d-1) return;
d[start.i][start.j]=init_d-1;
fuel[start.i][start.j]=0;
color[start.i][start.j]=init_d;
Q.push(start);
while(Q.size()){
node u=Q.front();Q.pop();
if(d[u.i][u.j]==bee[u.i][u.j] && fuel[u.i][u.j]==0) continue; //Bees caught Mecho
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(board[i][j]=='T') continue;
if(color[i][j]!=init_d){
d[i][j]=(fuel[u.i][u.j]>0)? d[u.i][u.j] : d[u.i][u.j]+1;
if(bee[i][j]<d[i][j]) continue; //Already filled with bees
fuel[i][j]=(fuel[u.i][u.j]>0)? fuel[u.i][u.j]-1 : s-1;
color[i][j]=init_d;
Q.push(node(i,j));
}
}
}
}
bool valid(int k)
{
bfsMecho(k);
return d[finish.i][finish.j]!=INT_MAX;
}
int binarySearch()
{
int k=0;
for(int b=10000;b>=1;b/=2){
while(valid(k+b)) k+=b;
}
return k-1;
}
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>>board[i][j];
if(board[i][j]=='M') start={i,j};
if(board[i][j]=='D') finish={i,j};
}
}
bfsBee();
int maximum=binarySearch();
cout<<maximum<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
512 KB |
Output is correct |
7 |
Correct |
148 ms |
10232 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
1152 KB |
Output is correct |
13 |
Correct |
1 ms |
896 KB |
Output is correct |
14 |
Correct |
1 ms |
1024 KB |
Output is correct |
15 |
Correct |
0 ms |
640 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
640 KB |
Output is correct |
18 |
Correct |
1 ms |
640 KB |
Output is correct |
19 |
Correct |
1 ms |
640 KB |
Output is correct |
20 |
Correct |
1 ms |
640 KB |
Output is correct |
21 |
Correct |
1 ms |
768 KB |
Output is correct |
22 |
Correct |
1 ms |
768 KB |
Output is correct |
23 |
Correct |
1 ms |
896 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
1024 KB |
Output is correct |
26 |
Correct |
1 ms |
1024 KB |
Output is correct |
27 |
Correct |
1 ms |
1024 KB |
Output is correct |
28 |
Correct |
1 ms |
1152 KB |
Output is correct |
29 |
Correct |
1 ms |
1152 KB |
Output is correct |
30 |
Correct |
1 ms |
1152 KB |
Output is correct |
31 |
Correct |
1 ms |
1152 KB |
Output is correct |
32 |
Correct |
1 ms |
1152 KB |
Output is correct |
33 |
Correct |
10 ms |
4352 KB |
Output is correct |
34 |
Correct |
11 ms |
4736 KB |
Output is correct |
35 |
Correct |
28 ms |
5024 KB |
Output is correct |
36 |
Correct |
15 ms |
5120 KB |
Output is correct |
37 |
Correct |
13 ms |
5376 KB |
Output is correct |
38 |
Correct |
39 ms |
5760 KB |
Output is correct |
39 |
Correct |
19 ms |
5760 KB |
Output is correct |
40 |
Correct |
18 ms |
6144 KB |
Output is correct |
41 |
Correct |
49 ms |
6400 KB |
Output is correct |
42 |
Correct |
26 ms |
6264 KB |
Output is correct |
43 |
Correct |
22 ms |
6784 KB |
Output is correct |
44 |
Correct |
60 ms |
7040 KB |
Output is correct |
45 |
Correct |
35 ms |
6784 KB |
Output is correct |
46 |
Correct |
29 ms |
7424 KB |
Output is correct |
47 |
Correct |
72 ms |
7680 KB |
Output is correct |
48 |
Correct |
43 ms |
7424 KB |
Output is correct |
49 |
Correct |
36 ms |
8192 KB |
Output is correct |
50 |
Correct |
91 ms |
8440 KB |
Output is correct |
51 |
Correct |
56 ms |
8064 KB |
Output is correct |
52 |
Correct |
51 ms |
8952 KB |
Output is correct |
53 |
Correct |
118 ms |
9208 KB |
Output is correct |
54 |
Correct |
71 ms |
8576 KB |
Output is correct |
55 |
Correct |
58 ms |
9592 KB |
Output is correct |
56 |
Correct |
145 ms |
9728 KB |
Output is correct |
57 |
Correct |
100 ms |
9336 KB |
Output is correct |
58 |
Correct |
84 ms |
10240 KB |
Output is correct |
59 |
Correct |
171 ms |
10444 KB |
Output is correct |
60 |
Correct |
115 ms |
9848 KB |
Output is correct |
61 |
Correct |
91 ms |
10872 KB |
Output is correct |
62 |
Correct |
198 ms |
11128 KB |
Output is correct |
63 |
Correct |
141 ms |
11000 KB |
Output is correct |
64 |
Correct |
223 ms |
11156 KB |
Output is correct |
65 |
Correct |
210 ms |
11000 KB |
Output is correct |
66 |
Correct |
196 ms |
11000 KB |
Output is correct |
67 |
Correct |
151 ms |
11076 KB |
Output is correct |
68 |
Correct |
73 ms |
11128 KB |
Output is correct |
69 |
Correct |
61 ms |
11160 KB |
Output is correct |
70 |
Correct |
59 ms |
11128 KB |
Output is correct |
71 |
Correct |
54 ms |
11128 KB |
Output is correct |
72 |
Correct |
48 ms |
9208 KB |
Output is correct |
73 |
Correct |
54 ms |
11388 KB |
Output is correct |
74 |
Correct |
83 ms |
11384 KB |
Output is correct |
75 |
Correct |
98 ms |
11384 KB |
Output is correct |
76 |
Correct |
96 ms |
11384 KB |
Output is correct |
77 |
Correct |
88 ms |
11384 KB |
Output is correct |
78 |
Correct |
96 ms |
10024 KB |
Output is correct |
79 |
Correct |
69 ms |
9592 KB |
Output is correct |
80 |
Correct |
90 ms |
9800 KB |
Output is correct |
81 |
Correct |
120 ms |
9848 KB |
Output is correct |
82 |
Correct |
89 ms |
9848 KB |
Output is correct |
83 |
Correct |
114 ms |
9976 KB |
Output is correct |
84 |
Correct |
110 ms |
9848 KB |
Output is correct |
85 |
Correct |
121 ms |
9976 KB |
Output is correct |
86 |
Correct |
116 ms |
9976 KB |
Output is correct |
87 |
Correct |
131 ms |
10104 KB |
Output is correct |
88 |
Correct |
111 ms |
10232 KB |
Output is correct |
89 |
Correct |
127 ms |
9976 KB |
Output is correct |
90 |
Correct |
163 ms |
10104 KB |
Output is correct |
91 |
Correct |
144 ms |
10104 KB |
Output is correct |
92 |
Correct |
139 ms |
10104 KB |
Output is correct |