#include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vb vector<bool>
#define vc vector<char>
#define vvc vector<vc>
#define vvb vector<vb>
#define si set<int>
#define mii map<int,int>
const int mod=1e9+7;
const int N=2e5+1;
const int LN=LLONG_MAX/10;
vvc L;
int n,k;
vvi Bees;
vvi Bear;
vpii BeesLoc;
bool inside(int y,int x){
return (y>=0 and y<n and x>=0 and x<n and L[y][x]!='T');
}
void bfsBees(){
queue<pii> Q;
for(auto B : BeesLoc){
Q.push(B);
Bees[B.first][B.second]=-1;
}
while(Q.size()){
auto B=Q.front();
int x=B.second;
int y=B.first;
Q.pop();
if(inside(y-1,x) and L[y-1][x]!='D' and Bees[y-1][x]>Bees[y][x]+1){
Bees[y-1][x]=Bees[y][x]+1;
Q.push({y-1,x});
}
if(inside(y+1,x) and L[y+1][x]!='D' and Bees[y+1][x]>Bees[y][x]+1){
Bees[y+1][x]=Bees[y][x]+1;
Q.push({y+1,x});
}
if(inside(y,x-1) and L[y][x-1]!='D' and Bees[y][x-1]>Bees[y][x]+1){
Bees[y][x-1]=Bees[y][x]+1;
Q.push({y,x-1});
}
if(inside(y,x+1) and L[y][x+1]!='D' and Bees[y][x+1]>Bees[y][x]+1){
Bees[y][x+1]=Bees[y][x]+1;
Q.push({y,x+1});
}
}
}
void bfsBear(int sy,int sx,int w){
queue<pii> Q;
Q.push({sy,sx});
Bear[sy][sx]=0;
if(Bees[sy][sx]<w)return;
while(Q.size()){
auto B=Q.front();
int x=B.second;
int y=B.first;
Q.pop();
if(inside(y-1,x) and Bear[y-1][x]>Bear[y][x]+1 and Bees[y-1][x]-((Bear[y][x]+1)/k)>=w){
Bear[y-1][x]=Bear[y][x]+1;
Q.push({y-1,x});
}
if(inside(y+1,x) and Bear[y+1][x]>Bear[y][x]+1 and Bees[y+1][x]-((Bear[y][x]+1)/k)>=w){
Bear[y+1][x]=Bear[y][x]+1;
Q.push({y+1,x});
}
if(inside(y,x-1) and Bear[y][x-1]>Bear[y][x]+1 and Bees[y][x-1]-((Bear[y][x]+1)/k)>=w){
Bear[y][x-1]=Bear[y][x]+1;
Q.push({y,x-1});
}
if(inside(y,x+1) and Bear[y][x+1]>Bear[y][x]+1 and Bees[y][x+1]-((Bear[y][x]+1)/k)>=w){
Bear[y][x+1]=Bear[y][x]+1;
Q.push({y,x+1});
}
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>k;
L=vvc(n,vc(n));
Bear=vvi(n,vi(n,LN));
Bees=vvi(n,vi(n,LN));
int sx,sy,ex,ey;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>L[i][j];
if(L[i][j]=='M')sy=i,sx=j;
if(L[i][j]=='D')ey=i,ex=j;
if(L[i][j]=='H')BeesLoc.emplace_back(i,j);
}
}
bfsBees();
int high=LN;
int low=0;
int res=-1;
while(high>=low){
int mid=(high+low)/2;
Bear=vvi(n,vi(n,LN));
bfsBear(sy,sx,mid);
if(Bear[ey][ex]==LN)high=mid-1;
else{
res=mid;
low=mid+1;
}
}
cout<<res;
}
Compilation message
mecho.cpp: In function 'int main()':
mecho.cpp:109:19: warning: 'ey' may be used uninitialized in this function [-Wmaybe-uninitialized]
109 | if(Bear[ey][ex]==LN)high=mid-1;
| ^
mecho.cpp:109:23: warning: 'ex' may be used uninitialized in this function [-Wmaybe-uninitialized]
109 | if(Bear[ey][ex]==LN)high=mid-1;
| ^
mecho.cpp:108:16: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
108 | bfsBear(sy,sx,mid);
| ~~~~~~~^~~~~~~~~~~
mecho.cpp:108:16: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
157 ms |
16228 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
264 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
14 ms |
3460 KB |
Output is correct |
34 |
Correct |
15 ms |
3368 KB |
Output is correct |
35 |
Correct |
29 ms |
3376 KB |
Output is correct |
36 |
Correct |
20 ms |
4396 KB |
Output is correct |
37 |
Correct |
20 ms |
4360 KB |
Output is correct |
38 |
Correct |
37 ms |
4396 KB |
Output is correct |
39 |
Correct |
25 ms |
5340 KB |
Output is correct |
40 |
Correct |
26 ms |
5468 KB |
Output is correct |
41 |
Correct |
50 ms |
5440 KB |
Output is correct |
42 |
Correct |
33 ms |
6604 KB |
Output is correct |
43 |
Correct |
39 ms |
6604 KB |
Output is correct |
44 |
Correct |
65 ms |
6516 KB |
Output is correct |
45 |
Correct |
42 ms |
7864 KB |
Output is correct |
46 |
Correct |
46 ms |
7900 KB |
Output is correct |
47 |
Correct |
77 ms |
7944 KB |
Output is correct |
48 |
Correct |
43 ms |
9276 KB |
Output is correct |
49 |
Correct |
44 ms |
9264 KB |
Output is correct |
50 |
Correct |
103 ms |
9284 KB |
Output is correct |
51 |
Correct |
53 ms |
10816 KB |
Output is correct |
52 |
Correct |
54 ms |
10884 KB |
Output is correct |
53 |
Correct |
121 ms |
10836 KB |
Output is correct |
54 |
Correct |
71 ms |
12520 KB |
Output is correct |
55 |
Correct |
70 ms |
12500 KB |
Output is correct |
56 |
Correct |
152 ms |
12520 KB |
Output is correct |
57 |
Correct |
80 ms |
14372 KB |
Output is correct |
58 |
Correct |
80 ms |
14296 KB |
Output is correct |
59 |
Correct |
176 ms |
14420 KB |
Output is correct |
60 |
Correct |
100 ms |
16176 KB |
Output is correct |
61 |
Correct |
100 ms |
16192 KB |
Output is correct |
62 |
Correct |
196 ms |
16180 KB |
Output is correct |
63 |
Correct |
173 ms |
16396 KB |
Output is correct |
64 |
Correct |
234 ms |
16184 KB |
Output is correct |
65 |
Correct |
215 ms |
16184 KB |
Output is correct |
66 |
Correct |
191 ms |
16192 KB |
Output is correct |
67 |
Correct |
183 ms |
16232 KB |
Output is correct |
68 |
Correct |
124 ms |
16196 KB |
Output is correct |
69 |
Correct |
119 ms |
16244 KB |
Output is correct |
70 |
Correct |
111 ms |
16304 KB |
Output is correct |
71 |
Correct |
109 ms |
16284 KB |
Output is correct |
72 |
Correct |
99 ms |
16232 KB |
Output is correct |
73 |
Correct |
111 ms |
16272 KB |
Output is correct |
74 |
Correct |
115 ms |
16248 KB |
Output is correct |
75 |
Correct |
130 ms |
16264 KB |
Output is correct |
76 |
Correct |
129 ms |
16308 KB |
Output is correct |
77 |
Correct |
127 ms |
16232 KB |
Output is correct |
78 |
Correct |
128 ms |
16236 KB |
Output is correct |
79 |
Correct |
122 ms |
16284 KB |
Output is correct |
80 |
Correct |
128 ms |
16276 KB |
Output is correct |
81 |
Correct |
136 ms |
16312 KB |
Output is correct |
82 |
Correct |
131 ms |
16252 KB |
Output is correct |
83 |
Correct |
161 ms |
16236 KB |
Output is correct |
84 |
Correct |
137 ms |
16312 KB |
Output is correct |
85 |
Correct |
143 ms |
16260 KB |
Output is correct |
86 |
Correct |
141 ms |
16228 KB |
Output is correct |
87 |
Correct |
140 ms |
16188 KB |
Output is correct |
88 |
Correct |
157 ms |
16240 KB |
Output is correct |
89 |
Correct |
152 ms |
16236 KB |
Output is correct |
90 |
Correct |
153 ms |
16256 KB |
Output is correct |
91 |
Correct |
156 ms |
16204 KB |
Output is correct |
92 |
Correct |
156 ms |
16276 KB |
Output is correct |