#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=0,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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
507 ms |
2576 KB |
Output is correct |
8 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Incorrect |
2 ms |
508 KB |
Output isn't correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
344 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
3 ms |
344 KB |
Output is correct |
28 |
Correct |
2 ms |
348 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 |
488 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
53 ms |
604 KB |
Output is correct |
34 |
Correct |
44 ms |
604 KB |
Output is correct |
35 |
Correct |
58 ms |
604 KB |
Output is correct |
36 |
Correct |
70 ms |
800 KB |
Output is correct |
37 |
Correct |
66 ms |
812 KB |
Output is correct |
38 |
Correct |
76 ms |
604 KB |
Output is correct |
39 |
Correct |
88 ms |
856 KB |
Output is correct |
40 |
Correct |
79 ms |
856 KB |
Output is correct |
41 |
Correct |
102 ms |
856 KB |
Output is correct |
42 |
Correct |
114 ms |
860 KB |
Output is correct |
43 |
Correct |
98 ms |
856 KB |
Output is correct |
44 |
Correct |
133 ms |
856 KB |
Output is correct |
45 |
Correct |
145 ms |
1116 KB |
Output is correct |
46 |
Correct |
124 ms |
1112 KB |
Output is correct |
47 |
Correct |
186 ms |
1116 KB |
Output is correct |
48 |
Correct |
164 ms |
1240 KB |
Output is correct |
49 |
Correct |
145 ms |
1256 KB |
Output is correct |
50 |
Correct |
192 ms |
1116 KB |
Output is correct |
51 |
Correct |
203 ms |
1372 KB |
Output is correct |
52 |
Correct |
179 ms |
1620 KB |
Output is correct |
53 |
Correct |
222 ms |
1368 KB |
Output is correct |
54 |
Correct |
237 ms |
1484 KB |
Output is correct |
55 |
Correct |
210 ms |
1700 KB |
Output is correct |
56 |
Correct |
258 ms |
1372 KB |
Output is correct |
57 |
Correct |
280 ms |
1648 KB |
Output is correct |
58 |
Correct |
233 ms |
1648 KB |
Output is correct |
59 |
Correct |
296 ms |
1624 KB |
Output is correct |
60 |
Correct |
311 ms |
1816 KB |
Output is correct |
61 |
Correct |
277 ms |
1820 KB |
Output is correct |
62 |
Correct |
344 ms |
1864 KB |
Output is correct |
63 |
Correct |
389 ms |
1848 KB |
Output is correct |
64 |
Correct |
426 ms |
1856 KB |
Output is correct |
65 |
Correct |
379 ms |
1628 KB |
Output is correct |
66 |
Correct |
434 ms |
1848 KB |
Output is correct |
67 |
Incorrect |
411 ms |
1628 KB |
Output isn't correct |
68 |
Correct |
453 ms |
2132 KB |
Output is correct |
69 |
Correct |
394 ms |
2024 KB |
Output is correct |
70 |
Correct |
462 ms |
2036 KB |
Output is correct |
71 |
Correct |
472 ms |
2284 KB |
Output is correct |
72 |
Correct |
500 ms |
1884 KB |
Output is correct |
73 |
Correct |
429 ms |
3980 KB |
Output is correct |
74 |
Correct |
535 ms |
3824 KB |
Output is correct |
75 |
Correct |
585 ms |
3816 KB |
Output is correct |
76 |
Correct |
585 ms |
3964 KB |
Output is correct |
77 |
Correct |
575 ms |
3832 KB |
Output is correct |
78 |
Incorrect |
533 ms |
3420 KB |
Output isn't correct |
79 |
Correct |
529 ms |
3920 KB |
Output is correct |
80 |
Correct |
558 ms |
3600 KB |
Output is correct |
81 |
Correct |
595 ms |
3612 KB |
Output is correct |
82 |
Correct |
544 ms |
3644 KB |
Output is correct |
83 |
Correct |
555 ms |
3280 KB |
Output is correct |
84 |
Correct |
570 ms |
3408 KB |
Output is correct |
85 |
Correct |
579 ms |
3288 KB |
Output is correct |
86 |
Correct |
574 ms |
3288 KB |
Output is correct |
87 |
Correct |
563 ms |
3432 KB |
Output is correct |
88 |
Correct |
530 ms |
2908 KB |
Output is correct |
89 |
Correct |
565 ms |
3052 KB |
Output is correct |
90 |
Correct |
554 ms |
2944 KB |
Output is correct |
91 |
Correct |
521 ms |
2904 KB |
Output is correct |
92 |
Correct |
524 ms |
3064 KB |
Output is correct |