# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
883250 | Servant_of_the_Lord | Mecho (IOI09_mecho) | C++17 | 595 ms | 4008 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;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)
{
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});
}
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});
}
if(o)l=m;
else h=m-1;
}
cout<<l<<'\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |