Submission #708870

#TimeUsernameProblemLanguageResultExecution timeMemory
708870hafoMecho (IOI09_mecho)C++17
19 / 100
548 ms9452 KiB
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define all(x) x.begin(), x.end()
using namespace std;

template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}

const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 8e2 + 7;
const ll oo = 1e17 + 69;

int n, s, xm, ym, xd, yd, vis[maxn][maxn];
char a[maxn][maxn];
pa d[] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

bool bfs(int time) {
    memset(vis, 0, sizeof vis);
    deque<pa> q;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(a[i][j] == 'H') {
                q.pb({i, j});
                vis[i][j] = 2;
            }
    q.push_front({xm, ym});
    vis[xm][ym] = 1;
    int cnt = 0;
    bool ok = (time == 0);
    while(!q.empty()) {
        int u, v;
        vector<pa> cur;
        cnt++;
        if(cnt <= time && !ok) {
            while(vis[q.back().fi][q.back().se] == 2) {
                u = q.back().fi, v = q.back().se;
                q.pop_back();
                cur.pb({u, v});
            }
        }
        if(ok) {
            if(cnt <= s) {
                while(vis[q.front().fi][q.front().se] == 1) {
                    u = q.front().fi, v = q.front().se;
                    q.pop_front();
                    cur.pb({u, v});
                }
            } else if(cnt == s + 1) {
                while(vis[q.back().fi][q.back().se] == 2) {
                    u = q.back().fi, v = q.back().se;
                    q.pop_back();
                    cur.pb({u, v});
                }
                cnt = 0;
            }
        }

        vector<pa> nxt;
        bool type = 0;
        for(auto e:cur) {
            u = e.fi, v = e.se;
            for(int k = 0; k < 4; k++) {
                int x = u + d[k].fi;
                int y = v + d[k].se;
                if(x < 0 || x == n || y < 0 || y == n || a[x][y] == 'T' || vis[x][y]) continue;
                if(vis[u][v] == 1 && a[x][y] == 'H') continue;
                if(vis[u][v] == 2 && a[x][y] == 'D') continue;
                vis[x][y] = vis[u][v];
                if(vis[x][y] == 1) type = 1;
                nxt.pb({x, y});
            }
        }
        for(auto e:nxt) {
            if(type) q.push_front(e);
            else q.pb(e);
        }
        if(cnt == time && !ok) {
            ok = 1;
            cnt = 0;
        }
    }
//    for(int i = 0; i < n; i++) {
//        for(int j = 0; j < n; j++) cout<<vis[i][j]<<" ";
//        cout<<"\n";
//    }
    return (vis[xd][yd] == 1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    //freopen(TASK".inp", "r", stdin);
    //freopen(TASK".out", "w", stdout);

    cin>>n>>s;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++) {
            cin>>a[i][j];
            if(a[i][j] == 'M') {
                xm = i;
                ym = j;
            }
            if(a[i][j] == 'D') {
                xd = i;
                yd = j;
            }
        }
    int l = 0, r = n * n, mid, res = -1;
    while(l <= r) {
        mid = l + r >> 1;
        if(bfs(mid)) {
            l = mid + 1;
            res = mid;
        } else r = mid - 1;
    }
    cout<<res;
    return 0;
}

Compilation message (stderr)

mecho.cpp: In function 'int main()':
mecho.cpp:119:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  119 |         mid = l + r >> 1;
      |               ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...