Submission #1276140

#TimeUsernameProblemLanguageResultExecution timeMemory
1276140mnnit_prakhargMecho (IOI09_mecho)C++20
53 / 100
148 ms4100 KiB
#pragma GCC optimize("O3", "inline")
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
#define pb push_back
#define ub upper_bound

const ll INF = 1e18 + 100;

vector<int> ra = {-1, 0, 1, 0};
vector<int> ca = {0, 1, 0, -1};

int f(int n, vector<string> &a, int s, int m, vector<int> &sp, vector<int> &ep)
{
    if (sp[0] == ep[0] && sp[1] == ep[1])
        return 1;
    queue<pair<int, int>> q1;
    vector<vector<int>> v(n, vector<int>(n, 0));
    v[ep[0]][ep[1]] = 1;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] == 'H')
            {
                q1.push({i, j});
                v[i][j] = 1;
            }
            else if (a[i][j] == 'T')
                v[i][j] = 1;
        }
    for (int i = 0; i < m; i++)
    {
        int sz = q1.size();
        while (sz-- > 0)
        {
            auto del = q1.front();
            q1.pop();
            for (int d = 0; d < 4; d++)
            {
                int nr = del.first + ra[d], nc = del.second + ca[d];
                if (nr >= 0 && nr < n && nc >= 0 && nc < n && v[nr][nc] == 0)
                {
                    q1.push({nr, nc});
                    v[nr][nc] = 1;
                }
            }
        }
    }
    if (v[sp[0]][sp[1]] == 1)
        return 0;
    queue<pair<int, int>> q2;
    q2.push({sp[0], sp[1]});
    v[sp[0]][sp[1]] = 2;
    while (q2.size())
    {
        int sz;
        for (int i = 0; i < s; i++)
        {
            sz = q2.size();
            while (sz-- > 0)
            {
                auto del = q2.front();
                q2.pop();
                if (v[del.first][del.second] == 1)
                    continue;
                for (int d = 0; d < 4; d++)
                {
                    int nr = del.first + ra[d], nc = del.second + ca[d];
                    if (nr >= 0 && nr < n && nc >= 0 && nc < n)
                    {
                        if (nr == ep[0] && nc == ep[1])
                            return 1;
                        if (v[nr][nc] == 0)
                        {
                            q2.push({nr, nc});
                            v[nr][nc] = 2;
                        }
                    }
                }
            }
        }

        sz = q1.size();
        while (sz-- > 0)
        {
            auto del = q1.front();
            q1.pop();
            for (int d = 0; d < 4; d++)
            {
                int nr = del.first + ra[d], nc = del.second + ca[d];
                if (nr >= 0 && nr < n && nc >= 0 && nc < n && v[nr][nc] != 1)
                {
                    q1.push({nr, nc});
                    v[nr][nc] = 1;
                }
            }
        }
    }
    return 0;
}

ll solve()
{
    int n, s;
    cin >> n >> s;
    vector<string> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i];
    vector<int> sp(2), ep(2);
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            if (a[i][j] == 'M')
                sp = {i, j};
            if (a[i][j] == 'D')
                ep = {i, j};
        }
    int l = 0, h = 2 * n, ma = -1;
    while (l <= h)
    {
        int m = (l + h) / 2;
        if (f(n, a, s, m, sp, ep))
        {
            ma = m;
            l = m + 1;
        }
        else
            h = m - 1;
    }
    return ma;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t--)
    {
        ll x = solve();
        cout << x << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...