#include <bits/stdc++.h>
#define ll long long
using namespace std;
main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll x,y,z,a,b,c;
cin>>x>>y;
vector<string>v;
vector<vector<ll>>w;
for(ll i=0;i<x;i++)
{
string s;
cin>>s;
v.push_back(s);
for(ll j=0;j<x;j++)
{
if(v[i][j]=='M')a=i,b=j;
if(v[i][j]=='H')w.push_back({i,j});
}
}
ll l=-1,h=x*x;
while(l<h)
{
ll m=l+(h-l+1)/2;
vector<string>u;
for(string i:v)u.push_back(i);
queue<vector<ll>>q,r;
for(vector<ll>i:w)q.push(i);
r.push({a,b});
q.push({-1,-1});
r.push({-1,-1});
bool o=false;
function<void(ll,ll)>a1=[&](ll a,ll b)
{
if(a<0||a>=x||b<0||b>=x||u[a][b]=='H'||u[a][b]=='D'||u[a][b]=='T')return;
u[a][b]='H';
q.push({a,b});
};
function<void(ll,ll)>a2=[&](ll a,ll b)
{
if(a<0||a>=x||b<0||b>=x||u[a][b]=='H'||u[a][b]=='M'||u[a][b]=='T')return;
if(u[a][b]=='D')o=true;
u[a][b]='M';
r.push({a,b});
};
for(ll i=0;i<m-1;i++)
{
while(q.front()[0]!=-1)
{
a1(q.front()[0]-1,q.front()[1]);
a1(q.front()[0]+1,q.front()[1]);
a1(q.front()[0],q.front()[1]-1);
a1(q.front()[0],q.front()[1]+1);
q.pop();
}
q.pop();
q.push({-1,-1});
}
while(r.front()[0]!=-1)
{
while(q.front()[0]!=-1)
{
a1(q.front()[0]-1,q.front()[1]);
a1(q.front()[0]+1,q.front()[1]);
a1(q.front()[0],q.front()[1]-1);
a1(q.front()[0],q.front()[1]+1);
q.pop();
}
q.pop();
q.push({-1,-1});
for(ll i=0;i<y;i++)
{
while(r.front()[0]!=-1)
{
if(u[r.front()[0]][r.front()[1]]!='M')
{
r.pop();
continue;
}
a2(r.front()[0]-1,r.front()[1]);
a2(r.front()[0]+1,r.front()[1]);
a2(r.front()[0],r.front()[1]-1);
a2(r.front()[0],r.front()[1]+1);
r.pop();
}
r.pop();
r.push({-1,-1});
}
}
if(o)l=m;
else h=m-1;
}
cout<<l<<'\n';
}
Compilation message
mecho.cpp:6:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
6 | main()
| ^~~~
mecho.cpp: In function 'int main()':
mecho.cpp:11:12: warning: unused variable 'z' [-Wunused-variable]
11 | ll x,y,z,a,b,c;
| ^
mecho.cpp:11:18: warning: unused variable 'c' [-Wunused-variable]
11 | ll x,y,z,a,b,c;
| ^
mecho.cpp:35:21: warning: 'b' may be used uninitialized in this function [-Wmaybe-uninitialized]
35 | r.push({a,b});
| ^
mecho.cpp:35:21: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
561 ms |
2576 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
2 ms |
500 KB |
Output is correct |
28 |
Correct |
2 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
344 KB |
Output is correct |
33 |
Correct |
53 ms |
604 KB |
Output is correct |
34 |
Correct |
46 ms |
724 KB |
Output is correct |
35 |
Correct |
60 ms |
604 KB |
Output is correct |
36 |
Correct |
70 ms |
808 KB |
Output is correct |
37 |
Correct |
65 ms |
828 KB |
Output is correct |
38 |
Correct |
76 ms |
604 KB |
Output is correct |
39 |
Correct |
87 ms |
856 KB |
Output is correct |
40 |
Correct |
79 ms |
856 KB |
Output is correct |
41 |
Correct |
120 ms |
928 KB |
Output is correct |
42 |
Correct |
130 ms |
856 KB |
Output is correct |
43 |
Correct |
98 ms |
996 KB |
Output is correct |
44 |
Correct |
129 ms |
1024 KB |
Output is correct |
45 |
Correct |
145 ms |
1112 KB |
Output is correct |
46 |
Correct |
118 ms |
1112 KB |
Output is correct |
47 |
Correct |
151 ms |
1156 KB |
Output is correct |
48 |
Correct |
215 ms |
1112 KB |
Output is correct |
49 |
Correct |
154 ms |
1244 KB |
Output is correct |
50 |
Correct |
179 ms |
1112 KB |
Output is correct |
51 |
Correct |
221 ms |
1384 KB |
Output is correct |
52 |
Correct |
176 ms |
1372 KB |
Output is correct |
53 |
Correct |
241 ms |
1420 KB |
Output is correct |
54 |
Correct |
238 ms |
1644 KB |
Output is correct |
55 |
Correct |
203 ms |
1520 KB |
Output is correct |
56 |
Correct |
259 ms |
1368 KB |
Output is correct |
57 |
Correct |
277 ms |
1876 KB |
Output is correct |
58 |
Correct |
282 ms |
1660 KB |
Output is correct |
59 |
Correct |
307 ms |
1716 KB |
Output is correct |
60 |
Correct |
346 ms |
1800 KB |
Output is correct |
61 |
Correct |
263 ms |
1800 KB |
Output is correct |
62 |
Correct |
353 ms |
1872 KB |
Output is correct |
63 |
Incorrect |
416 ms |
1836 KB |
Output isn't correct |
64 |
Correct |
439 ms |
2092 KB |
Output is correct |
65 |
Correct |
393 ms |
1856 KB |
Output is correct |
66 |
Correct |
451 ms |
1848 KB |
Output is correct |
67 |
Correct |
419 ms |
1872 KB |
Output is correct |
68 |
Incorrect |
448 ms |
2040 KB |
Output isn't correct |
69 |
Correct |
404 ms |
1884 KB |
Output is correct |
70 |
Correct |
414 ms |
2040 KB |
Output is correct |
71 |
Correct |
474 ms |
2044 KB |
Output is correct |
72 |
Correct |
500 ms |
2032 KB |
Output is correct |
73 |
Correct |
444 ms |
3820 KB |
Output is correct |
74 |
Correct |
552 ms |
3924 KB |
Output is correct |
75 |
Correct |
620 ms |
3912 KB |
Output is correct |
76 |
Correct |
571 ms |
3828 KB |
Output is correct |
77 |
Correct |
588 ms |
4064 KB |
Output is correct |
78 |
Correct |
543 ms |
3604 KB |
Output is correct |
79 |
Correct |
535 ms |
3672 KB |
Output is correct |
80 |
Correct |
576 ms |
3608 KB |
Output is correct |
81 |
Correct |
554 ms |
3616 KB |
Output is correct |
82 |
Correct |
547 ms |
3788 KB |
Output is correct |
83 |
Incorrect |
542 ms |
3276 KB |
Output isn't correct |
84 |
Correct |
540 ms |
3416 KB |
Output is correct |
85 |
Correct |
573 ms |
3408 KB |
Output is correct |
86 |
Correct |
540 ms |
3288 KB |
Output is correct |
87 |
Correct |
536 ms |
3412 KB |
Output is correct |
88 |
Incorrect |
533 ms |
3156 KB |
Output isn't correct |
89 |
Correct |
522 ms |
2948 KB |
Output is correct |
90 |
Correct |
525 ms |
3160 KB |
Output is correct |
91 |
Correct |
559 ms |
3160 KB |
Output is correct |
92 |
Correct |
563 ms |
3116 KB |
Output is correct |