Submission #1187808

#TimeUsernameProblemLanguageResultExecution timeMemory
1187808grimreaperMecho (IOI09_mecho)C++20
100 / 100
203 ms11248 KiB
#ifndef _Alignof
#define _Alignof(x) __alignof__(x)
#endif

#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define vll vector<long long>
#define vpll vector< pair<long long int , long long int > > 
#define rep(i,x,a) for (long long i=x; i<a; i++)
#define si(x) (long long int)x.size()
#define pb push_back
#define ff first
#define ss second
#define lg2(x) (long long int)log2(x)
#define gap " "
#define nl '\n' 
#define all(x) (x).begin(),(x).end()
#define no cout<<"No\n"
#define yes cout<<"Yes\n"
#define vld vector<long double>

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
void print(){
    cout << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
    cout << a;
    (cout << ... << (cout << ' ', b));
    cout << '\n';
}
template<class... T>
void input(T&... a){
    (cin >> ... >> a);
}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

const int MOD=1e9+7;
const int N=1e6+10;;
const ll INF=1e16;
const long long int  LINF=LLONG_MAX;

/*------------------------------------*/

void solve(int tc = 0) 
{
    ll n,s;input(n,s);
    vector<vector<char>> grid(n,vector<char>(n));
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin >> grid[i][j];
        }
    }
    vector<vector<ll>> dis(n,vll(n,INF));
    queue<pair<ll,ll>> q;
    vll dx = {0,0,1,-1};
    vll dy = {1,-1,0,0};
    ll xd = 0,yd = 0;
    ll xm = 0,ym = 0;
    rep(i,0,n)rep(j,0,n)if(grid[i][j]=='M')xm = i,ym = j;
    rep(i,0,n)rep(j,0,n)if(grid[i][j]=='D')xd = i,yd = j;
    rep(i,0,n)rep(j,0,n)if(grid[i][j]=='H')dis[i][j] = 0,q.push({i,j});
    while(!q.empty()){
        auto [x,y] = q.front();
        q.pop();
        for(int i=0;i<4;i++){
            ll newx = x+dx[i];
            ll newy = y+dy[i];
            if(newx >= 0 && newy >=0 && newx < n && newy < n && dis[newx][newy] > dis[x][y]+1 && grid[newx][newy]!='T' && grid[newx][newy]!='D'){
                dis[newx][newy] = dis[x][y]+1;
                q.push({newx,newy});
            }
        }
    }
    auto check = [&](ll mid)->bool{
        vector<vector<ll>> mecho(n,vll(n,INF));
        if(dis[xm][ym] <= mid){
            return false;
        }
        mecho[xm][ym] = mid;
        queue<array<ll,3>>q;q.push({xm,ym,0});
        while(!q.empty()){
            auto [x,y,step] = q.front();
            q.pop();step++;
            if(x==xd && y==yd){
                return true;
            }
            for(int i=0;i<4;i++){
                ll newx = x+dx[i];
                ll newy = y+dy[i];
                if(newx >= 0 && newy >= 0 && newx < n && newy < n && grid[newx][newy]!='T' && grid[newx][newy]!='H' && mecho[newx][newy]>mecho[x][y]+(step%s==0) && mecho[x][y]+(step%s==0) < dis[newx][newy]){
                    mecho[newx][newy] = mecho[x][y]+(step%s==0);
                    q.push({newx,newy,step});
                }
            }
        }
        return false;
    };
    ll low = 0;
    ll high = INF;
    ll ans = -1;
    while(low <= high){
        ll mid = (low+high)/2;
        if(check(mid)){
            ans = max(ans,mid);
            low = mid+1;
        }
        else{
            high = mid-1;
        }
    }
    cout << ans << nl;
}

/*------------------------------------*/

int main()
{   
#ifndef ONLINE_JUDGE
    freopen("Error.txt","w",stderr);
#endif
    ios_base::sync_with_stdio(false); 
    cin.tie(nullptr); cout.tie(nullptr);  
    ll tc=1;
    for(int i=0 ; i < tc ; i++)
    {
        solve(i);
    }
return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'int main()':
mecho.cpp:153:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |     freopen("Error.txt","w",stderr);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...