제출 #870673

#제출 시각아이디문제언어결과실행 시간메모리
870673Essa2006Mecho (IOI09_mecho)C++17
84 / 100
181 ms6392 KiB
#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) && 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(Dis[i][j]/s+Md>=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=inf, 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;
    }
    cout<<ans;
    
}
#Verdict Execution timeMemoryGrader output
Fetching results...