# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
870684 | Essa2006 | Mecho (IOI09_mecho) | C++17 | 192 ms | 6484 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>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
const int inf=1e9;
int n, s, Md, can;
array<int, 2>Str, End;
vector<string>A;
vector<vector<int>>Pri;
queue<array<int, 2>>q, qu;
int dx[]={0, 0, 1, -1};
int dy[]={1, -1, 0, 0};
void pre(){
A.clear(), Pri.clear();
A.resize(n), Pri.resize(n, vector<int>(n, inf));
}
bool valid(int i, int j){
return !(i<0 || i>=n || j<0 || j>=n);
}
void bfs(){
while(!q.empty()){
array<int, 2>u=q.front();
q.pop();
int i=u[0], j=u[1];
for(int f=0;f<4;f++){
int ii=i+dx[f], jj=j+dy[f];
if(valid(ii, jj) && A[ii][jj]!='D' && Pri[i][j]+1<Pri[ii][jj])
Pri[ii][jj]=Pri[i][j]+1, q.push({ii, jj});
}
}
}
void check(){
vector<vector<int>>Dis(n, vector<int>(n, inf));
Dis[Str[0]][Str[1]]=0;
while(!qu.empty()){
array<int, 2>u=qu.front();
qu.pop();
int i=u[0], j=u[1];
if(Md+Dis[i][j]/s>=Pri[i][j]){
continue;
}
if(i==End[0] && j==End[1]){
can=1;
continue;
}
for(int f=0;f<4;f++){
int ii=i+dx[f], jj=j+dy[f];
if(valid(ii, jj) && Dis[i][j]+1<Dis[ii][jj]){
Dis[ii][jj]=Dis[i][j]+1, qu.push({ii, jj});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>s;
pre();
for(int i=0;i<n;i++){
cin>>A[i];
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(A[i][j]=='H')
q.push({i, j}), Pri[i][j]=0;
else if(A[i][j]=='M')
Str[0]=i, Str[1]=j;
else if(A[i][j]=='D')
End[0]=i, End[1]=j;
else if(A[i][j]=='T')
Pri[i][j]=0;
}
}
bfs();
int l=0, r=n*n, ans=-1;
while(l<=r){
int md=(l+r)/2;
Md=md;
can=0;
qu.push({Str[0], Str[1]});
check();
if(can)
ans=md, l=md+1;
else
r=md-1;
}
assert(Pri[End[0]][End[1]]==inf);
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |