#include <bits/stdc++.h>
using namespace std;
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;
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;
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 |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
512 KB |
Output is correct |
7 |
Correct |
152 ms |
10872 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 |
512 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 |
1024 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 |
11 ms |
4480 KB |
Output is correct |
34 |
Correct |
10 ms |
4864 KB |
Output is correct |
35 |
Correct |
29 ms |
5152 KB |
Output is correct |
36 |
Correct |
17 ms |
5248 KB |
Output is correct |
37 |
Correct |
14 ms |
5504 KB |
Output is correct |
38 |
Correct |
39 ms |
5888 KB |
Output is correct |
39 |
Correct |
29 ms |
5760 KB |
Output is correct |
40 |
Correct |
29 ms |
6272 KB |
Output is correct |
41 |
Correct |
57 ms |
6528 KB |
Output is correct |
42 |
Correct |
28 ms |
6400 KB |
Output is correct |
43 |
Correct |
30 ms |
7040 KB |
Output is correct |
44 |
Correct |
68 ms |
7296 KB |
Output is correct |
45 |
Correct |
35 ms |
7040 KB |
Output is correct |
46 |
Correct |
31 ms |
7808 KB |
Output is correct |
47 |
Correct |
77 ms |
8064 KB |
Output is correct |
48 |
Correct |
45 ms |
7808 KB |
Output is correct |
49 |
Correct |
40 ms |
8568 KB |
Output is correct |
50 |
Correct |
100 ms |
8704 KB |
Output is correct |
51 |
Correct |
62 ms |
8568 KB |
Output is correct |
52 |
Correct |
52 ms |
9216 KB |
Output is correct |
53 |
Correct |
126 ms |
9472 KB |
Output is correct |
54 |
Correct |
79 ms |
9172 KB |
Output is correct |
55 |
Correct |
63 ms |
10104 KB |
Output is correct |
56 |
Correct |
150 ms |
10232 KB |
Output is correct |
57 |
Correct |
94 ms |
9848 KB |
Output is correct |
58 |
Correct |
76 ms |
10872 KB |
Output is correct |
59 |
Correct |
169 ms |
11000 KB |
Output is correct |
60 |
Correct |
114 ms |
10488 KB |
Output is correct |
61 |
Correct |
93 ms |
11520 KB |
Output is correct |
62 |
Correct |
195 ms |
11716 KB |
Output is correct |
63 |
Correct |
139 ms |
11640 KB |
Output is correct |
64 |
Correct |
229 ms |
11768 KB |
Output is correct |
65 |
Correct |
209 ms |
11896 KB |
Output is correct |
66 |
Correct |
179 ms |
11640 KB |
Output is correct |
67 |
Correct |
137 ms |
11768 KB |
Output is correct |
68 |
Correct |
70 ms |
11768 KB |
Output is correct |
69 |
Correct |
58 ms |
11768 KB |
Output is correct |
70 |
Correct |
59 ms |
11768 KB |
Output is correct |
71 |
Correct |
50 ms |
11768 KB |
Output is correct |
72 |
Correct |
47 ms |
9848 KB |
Output is correct |
73 |
Correct |
49 ms |
11896 KB |
Output is correct |
74 |
Correct |
90 ms |
12024 KB |
Output is correct |
75 |
Correct |
103 ms |
11896 KB |
Output is correct |
76 |
Correct |
102 ms |
11972 KB |
Output is correct |
77 |
Correct |
106 ms |
11896 KB |
Output is correct |
78 |
Correct |
101 ms |
10616 KB |
Output is correct |
79 |
Correct |
82 ms |
10236 KB |
Output is correct |
80 |
Correct |
97 ms |
10364 KB |
Output is correct |
81 |
Correct |
109 ms |
10616 KB |
Output is correct |
82 |
Correct |
91 ms |
10484 KB |
Output is correct |
83 |
Correct |
111 ms |
10808 KB |
Output is correct |
84 |
Correct |
113 ms |
10556 KB |
Output is correct |
85 |
Correct |
113 ms |
10732 KB |
Output is correct |
86 |
Correct |
127 ms |
10632 KB |
Output is correct |
87 |
Correct |
132 ms |
10744 KB |
Output is correct |
88 |
Correct |
117 ms |
10772 KB |
Output is correct |
89 |
Correct |
124 ms |
10616 KB |
Output is correct |
90 |
Correct |
146 ms |
10876 KB |
Output is correct |
91 |
Correct |
150 ms |
10744 KB |
Output is correct |
92 |
Correct |
132 ms |
10744 KB |
Output is correct |